14

I'm trying to use wp_nav_menu to only display a menu if one exists, otherwise, display nothing.

If I delete the menu, it will output a list of the pages.

My functions.php file contains:

if (function_exists('register_nav_menus')) {
register_nav_menus (
array('main_nav' => 'Main Navigation Menu'));}

How can I use wp_nav_menu to only display a menu if one exists, otherwise show nothing?

4 Answers 4

24

Use has_nav_menu(), and test for theme_location, rather than menu_id:

<?php
if ( has_nav_menu( $theme_location ) ) {
    // User has assigned menu to this location;
    // output it
    wp_nav_menu( array( 
        'theme_location' => $theme_location, 
        'menu_class' => 'nav', 
        'container' => '' 
    ) );
}
?>

You can output alternate content, by adding an else clause.

EDIT

You need to replace $theme_location with your actual theme_location:

<?php
if ( has_nav_menu( 'main_nav' ) ) {
    // User has assigned menu to this location;
    // output it
    wp_nav_menu( array( 
        'theme_location' => 'main_nav', 
        'menu_class' => 'nav', 
        'container' => '' 
    ) );
}
?>
9
  • While this outputs nothing if there's no menu, it unfortunately does the same when there is a menu. Any other ideas?
    – Ryan
    Nov 2, 2011 at 21:23
  • Have you applied a menu to the specified Theme Location? Nov 2, 2011 at 21:28
  • No, how do I do that?
    – Ryan
    Nov 2, 2011 at 21:32
  • 1
    Dashboard -> Appearance -> Menus. Create and save a custom menu, then apply it to the specified Theme Location, using the dropdown menu. Nov 2, 2011 at 21:34
  • Ah yes, I had done that (screenshot: bit.ly/vrjNf9). Unfortunately, the problem still persists - the menu won't appear. P.S. My functions file has this: if (function_exists('register_nav_menus')) { register_nav_menus ( array('main_nav' => 'Main Navigation Menu'));}
    – Ryan
    Nov 2, 2011 at 21:37
8

You can just specify false as the fallback_cb argument of wp_nav_menu. Nothing will show up -- rather, wp_nav_menu will return false (echoing nothing out).

<?php
wp_nav_menu(array( 
    'theme_location' => $main_nav, 
    'menu_class'     => 'nav', 
    'container'      => '',
    'fallback_cb'    => false
));
2
0

You can just register menu firstly without specifying the location.
In functions.php:

add_action( 'init', 'register_my_menus' );
function register_my_menus() {  
    register_nav_menus(
        array(
            'header' => __( 'Header Menu' )
        )
    );
}

And when you call the menu in header.php, check with has_nav_menu():

if ( has_nav_menu( 'header' ) ) {
    wp_nav_menu( array( 'theme_location' => 'header' ) ); 
}
1
  • If you register a menu without a theme location, you cannot ask for it later with a theme location restriction.
    – fuxia
    Nov 27, 2012 at 4:33
0

Read this:

http://codex.wordpress.org/Function_Reference/wp_nav_menu

set 'fallback_cb' => false and none will be displayed, this is the standard method suggested by wp guys.

3
  • This is the same as chrisguitarguy answer...
    – brasofilo
    Oct 6, 2013 at 13:02
  • I know, I just wanted to point you out to the main article on wordpress codex. Oct 10, 2013 at 14:34
  • It seems that it is not true. The docs says: If not given a theme_location parameter, the function displays developer.wordpress.org/reference/functions/wp_nav_menu/… the menu matching the ID, slug, or name given by the menu parameter; otherwise, the first non-empty menu; otherwise (or if the menu given by menu is empty), output of the function given by the fallback_cb parameter (wp_page_menu(), by default); otherwise nothing.
    – Alireza
    Nov 6, 2018 at 14:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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