Skip Navigation

[Resolved] Dynamic sub-menu based on custom field

This support ticket is created 4 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 2 replies, has 2 voices.

Last updated by FelipeP5703 4 years, 10 months ago.

Assisted by: Waqar.

Author
Posts
#1460271

I'm wondering if there is a way to create a sub-menu by using one of the custom field, and only if there is anything assigned to it.

For example, on my website hidden link there is a menu bar with a couple of pages. I would like to create a sub-menu, once the mouse is over Terapeuta or Massagista, and it would show the locations available (dynamically). The locations are selection custom field but not all of them have custom posts.

Here is an example: hidden link

This way, the visitor can go straight to the neighborhood they want instead of having to go to the session then choose the neighborhood from there.

Thanks in advance

#1461461

Hi Felipe,

Thank you for contacting us and I'd be happy to assist.

There is no direct built-in feature available in Toolset plugins, which can insert the menu items in a navigation menu, dynamically.

It will require customization, with a good understanding and familiarity with PHP and WordPress navigation menu related functions.

As much as we would like to help, 1-1 custom code assistance of this scale is beyond the scope of support that can be provided over the forum. But, we do our best to guide in the right direction, whenever possible.

This requirement can be broken down into 3 main parts:

1. The URL structure of the sub-menu items:

The way the view on the "Terapeutas" page works ( hidden link ), you just need to specify the value of the neighborhood in the URL parameter "wpv-wpcf-bairro-rj", to make it pre-populate the listings from that neighborhood only.

For example:

Barra da Tijuca
hidden link

Botafogo
hidden link

2. Getting the labels of all the available neighborhood custom field labels and values:

For this, you can create a raw post view which loops through all the listing posts and only returns the available neighborhood custom field labels (e.g. Botafogo) and values (e.g. 14).
( ref: https://toolset.com/documentation/customizing-sites-using-php/functions/ )

The output of that view can then be called in a PHP code so that these labels and values can be used to create sub-menu item links.
( ref: https://toolset.com/documentation/programmer-reference/views-api/#render_view )

3. Adding sub-menu items, dynamically into a navigation menu:

Here are some useful WordPress functions which can be used for this:

- wp_get_nav_menu_items
https://developer.wordpress.org/reference/functions/wp_get_nav_menu_items/

- wp_create_nav_menu
https://developer.wordpress.org/reference/functions/wp_create_nav_menu/

- wp_update_nav_menu_item
https://developer.wordpress.org/reference/functions/wp_update_nav_menu_item/

Here is a thread with good usage example of programmatically generated navigation menu items:
https://wordpress.stackexchange.com/a/44739

I hope this helps and for more personalized assistance around custom code, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

#1462051

Thank you Waqar for all your help. I will study what you have sent me and try to implement it. My issue is resolved now. Thank you!