Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

4 Answers

up vote 7 down vote accepted

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' => '' 
    ) );
}
?>
share|improve this answer
    
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 '11 at 21:23
    
Have you applied a menu to the specified Theme Location? –  Chip Bennett Nov 2 '11 at 21:28
    
No, how do I do that? –  Ryan Nov 2 '11 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. –  Chip Bennett Nov 2 '11 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 '11 at 21:37
show 5 more comments

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
    )
);
share|improve this answer
add comment

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' ) ); 
}
share|improve this answer
    
If you register a menu without a theme location, you cannot ask for it later with a theme location restriction. –  toscho Nov 27 '12 at 4:33
add comment

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.

share|improve this answer
    
This is the same as chrisguitarguy answer... –  brasofilo Oct 6 '13 at 13:02
    
I know, I just wanted to point you out to the main article on wordpress codex. –  CapitalH.ir Oct 10 '13 at 14:34
add comment

Your Answer

 
discard

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.