Monday, 19 August 2019

Current level navigation in Wordpress

I have a 3 level navigation:

Home
   > submenu1
       >> sub3       
   > submenu
       >> sub4       
       >> sub5       // current page
About
   > about2
       >> sub6       
   > about3
       >> sub7       

I am trying to get each navigation level separately,

With the example nav above, being on the sub5 page, I would need

Current 1st level nav: Home

Current parallel 2nd level nav: submenu1, submenu (who are both under Home)

Current parallel 3rd level nav: sub4, sub5 (who are menu under submenu)

I will need to modify the elements and styles of the menu and will need to work with menu items in php, such as:

$menu_name = 'topnav';
if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_name ] ) ) {
$menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
$menu_items = wp_get_nav_menu_items($menu->term_id);

foreach ( (array) $menu_items as $key => $menu_item ) {
    $title = $menu_item->title;
    $url = $menu_item->url;
    [...]

Please help getting the navigation layers separately to show up as explained above.

I got the 1st and 3rd level working, but can't get the current parallel 2nd level nav to show up correctly.

Thanks.



from Current level navigation in Wordpress

No comments:

Post a Comment