I'm using wp_nav_menu
for a submenu that I only want to display (including the surrounding html) when there are sub-menu items to display.
The problem is, the function is always returning with either the menu items (when it's supposed to), or with an empty list when there's nothing to display. I need it to (and thought it would) return false.
Here's my code.
// Capture submenu as a variable
$args = array(
'theme_location' => $in_menu,
'echo' => '0',
'sub_menu' => true,
'fallback_cb' => 'coepio_return_false'
); // Note I've also tried 'fallback_cb' => false with the same result
$submenu = wp_nav_menu( $args );
// If the menu exists
if( $submenu ) {
?>
<div id="submenu" class="sidebar cf" role="navigation">
<div class="widget">
<div id="submenu-title" class="widget-title">
<h4>In This Section</h4>
</div>
<div class="widget-content">
<?php
echo $submenu;
?>
</div>
</div>
</div><!-- #submenu -->
<?php
}
If you noticed that 'sub_menu' => true
argument you have a good eye. Here's the code for that.
// Submenus - required for the submenu (above) to work
// filter_hook function to react on sub_menu flag
function coepio_submenu( $sorted_menu_items, $args ) {
if ( isset( $args->sub_menu ) ) {
$root_id = 0;
// find the current menu item
foreach ( $sorted_menu_items as $menu_item ) {
if ( $menu_item->current ) {
// set the root id based on whether the current menu item has a parent or not
$root_id = ( $menu_item->menu_item_parent ) ? $menu_item->menu_item_parent : $menu_item->ID;
break;
}
}
$menu_item_parents = array();
foreach ( $sorted_menu_items as $key => $item ) {
// init menu_item_parents
if ( $item->ID == $root_id ) $menu_item_parents[] = $item->ID;
if ( in_array( $item->menu_item_parent, $menu_item_parents ) ) {
// part of sub-tree: keep!
$menu_item_parents[] = $item->ID;
} else {
// not part of sub-tree: away with it!
unset( $sorted_menu_items[$key] );
}
}
return $sorted_menu_items;
} else {
return $sorted_menu_items;
}
}
add_filter( 'wp_nav_menu_objects', 'coepio_submenu', 10, 2 );