wp_nav_menu: show menu only if one exists, otherwise show nothing

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

Leave a Comment