WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 class="widget-content">
                echo $submenu;

    </div><!-- #submenu -->

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;

    $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 );
share|improve this question

it doesn't return false because the menu exists in every case, you're just unsetting all of the menu items. you're not setting an items_wrap arg, so that defaults to the ul wrapper. maybe try removing it by setting it explicitly to just the child items:

'items_wrap' => '%3$s'

note, you'll then have to manually wrap your menu in ul tags in that case.

share|improve this answer
Thanks, I managed to get it working by doing that, plus setting 'container' => falseand then checking for if( $submenu != '' ) – Angus Russell Jul 3 '13 at 8:11

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.