Multi-level wp_nav_menu return children
-
Hello,
Wondering if someone could point me in the right direction. I’m using wp_nav_menu with subpages to make a second-tier menu. I have split the template code so that the main nav always stays 1 level, but on pages that have subpages, a second menu appears inside the page template.
I’m using the following code and it works just fine, showing the child pages of the current page only if they exist. But when you go to the subpages, that subnav disappears. It reminds me of the old category highlighting on single.php problems of yore. Can anyone help me modify it so that the function not only returns child pages of the current page, but also returns them when viewing one of those children?
function my_custom_nav($elements) { global $wp_query; $queried_id = $wp_query->queried_object_id; $child_items = array(); $parents = array(); foreach( $elements as $key => $element ) { if( $element->object_id == $queried_id ) $parents[] = $element->ID; if( in_array($element->menu_item_parent, $parents) ) $child_items[] = $element; } return $child_items; }
and then in the template I call wp_nav_menu again. The code come from Utkarsh Kukreti’s comment in this article https://wpquestions.com/question/show/id/882
Paul
- The topic ‘Multi-level wp_nav_menu return children’ is closed to new replies.