I want to display only a given level of menu items in my main nav menu when on that page. Currently sub menus of my main menu are part of the main menu HTML being an unordered list within the nav link list.
<ul id="menu-main-top-navigation">
<li class="menu nav-1">
...
<ul>
<li class="menu sub-nav-1"></li>
<li class="menu sub-nav-2"></li>
</ul>
</li>
<li class="menu nav-2">...</li>
<li class="menu nav-3">...</li>
</ul>
When on the page of menu nav-1, I want something like
<ul id="menu-main-sub-navigation">
<li class="menu sub-nav-1">...</li>
<li class="menu sub-nav-2">...</li>
</ul>
Currently I have written a hacky piece of code which displays only the second level navigation for a given menu item when that page is open. However it doesn't work for any given level.
I think a custom walker is the way to go but I cannot seem to get it to work...
EDIT:
Here is my hacky code which currently only works to display the second level sub navigation when on that page;
function submenu_display( $items, $args ) {
$current_page_id = (string)get_the_ID();
$top_level = array();
$second_level = array();
foreach ( $items as $item ) {
// The parent menu ID is different to the object (post) ID
if ( $item->object_id === $current_page_id ) {
$current_parent_id = $item->ID;
}
if ( $item->menu_item_parent === '0' ) {
$top_level[ $item->ID ] = $item->object_id;
}
if ( array_key_exists( $item->menu_item_parent, $top_level ) ) {
$second_level[ $item->ID ] = $item->object_id;
}
}
if ( isset( $current_parent_id ) ) {
if ( array_key_exists( $current_parent_id, $second_level ) ) {
foreach ( $items as $item => $details ) {
if ( $current_parent_id === $details->ID ) {
$current_parent_id = $details->menu_item_parent;
}
}
}
if ( array_key_exists( $current_parent_id, $top_level ) ) {
foreach ( $items as $item => $details ) {
if ( $details->menu_item_parent != $current_parent_id ) {
unset( $items[ $item ] );
}
}
}
} else {
$items = array();
}
return $items;
}
And used via
add_filter( 'wp_nav_menu_objects', 'submenu_display', 10, 2 );
wp_nav_menu( array( 'theme_location' => 'primary_navigation',
'menu_class' => 'nav nav-sub',
'menu_id' => 'menu-main-sub-navigation'
) );
remove_filter( 'wp_nav_menu_objects', 'submenu_display', 10 );