Support » Themes and Templates » How to show wp_nav_menu only if not empty

  • benraaa

    (@benraaa)


    I need wp_nav_menu to show only if it has a menu item inside. Otherwise the menu as well as its wrapper should not appear at all. I thought this could have been done with the parameter $fallback_cb, if it is left empty. But it didn’t work. Maybe it’s possible to put something like if ( !empty () ); around it. But I don’t know how to call the menu for an existing item. Does somebody know how to do?

    My menu looks like this:

    <?php
    	wp_nav_menu( array( 'theme_location' => 'pin', 'fallback_cb' => '', 'container' => 'div', 'items_wrap' => '<ul><li class="start">++</li> %3$s <li class="end">++</li></ul>' ) );
     ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • Pioneer Web Design

    (@swansonphotos)

    I do not have a quick answer but I can maybe point you to one: If one creates a custom menu in WP3.3.2, and does not add anything to it, assigns it properly to a location, it does not show = there will be no nav menu there at all (at least in the Twenty Eleven theme), so such code exists there…

    benraaa

    (@benraaa)

    Thanks for your really quick reply. So I will have a look for the newest Twenty Eleven. It will take some hours, since I have to do some other business first. But I will answer then.

    [ Moderator Note: Please post code or markup snippets between backticks or use the code button. ]

    You can use the parameter fallback_cb but you would need to set it “false” like this:

    <?php
    	wp_nav_menu( array( 'theme_location' => 'pin', 'fallback_cb' => <strong>false</strong>, 'container' => 'div', 'items_wrap' => '
    <ul><li class="start">++ %3$s <li class="end">++</ul>
    ' ) );
     ?>

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to show wp_nav_menu only if not empty’ is closed to new replies.