WordPress.org

Ready to get started?Download WordPress

Forums

How to show wp_nav_menu only if not empty (4 posts)

  1. benraaa
    Member
    Posted 1 year ago #

    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>' ) );
     ?>
  2. Pioneer Valley Web Design
    Member
    Posted 1 year ago #

    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...

  3. benraaa
    Member
    Posted 1 year ago #

    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.

  4. cinematic
    Member
    Posted 1 year ago #

    [ 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>
    ' ) );
     ?>

Topic Closed

This topic has been closed to new replies.

About this Topic