I am new to WordPress, and am still trying to learn the in's and out's in much respect to this well known CMS.

I am wanting to know, if I should or must have a Fallback for main menu?

This is what I am working with:

// This theme uses wp_nav_menu() in one location.
register_nav_menus( array(
    'primary' => __( 'Primary Navigation', 'xxxxxx' ),
) );

And here is the callback within my header.php file:

    <?php wp_nav_menu( 
        'theme_location' => 'Primary', 
        'menu' =>  'Primary',
        'container' => '', 
        'items_wrap' => '<ul class="main">%3$s</ul>'  

Like I said, I am new. So please, any help will be much welcome.

And if you could, please respond with an example.

2 Answers 2


The parameter fallback_cb will be used when the user has no menu assigned to your theme_location. It should be a function that returns a string, but you can use __return_false() to suppress any output:

        'theme_location' => 'primary',
        // No menu available: no output.
        'fallback_cb'    => '__return_false'

You should always declare the fallback explicitly to keep your code readable.

Take the example from TwentyEleven:

/* Our navigation menu. If one isn't filled out, wp_nav_menu 
falls back to wp_page_menu. The menu assigned to the primary 
location is the one used. If one isn't assigned, the menu with 
the lowest ID is used. */ 
<?php wp_nav_menu( array( 'theme_location' => 'primary' ) ); ?>

That’s very confusing. One could think that the theme will now use the first available custom menu if there was no menu assigned to this theme location. Wrong. The list of pages will be used instead. Declaring a callback would have been much easier to understand.

The fallback will get all the menu arguments. You have to inspect the echo parameter, because WordPress will not take care of that for you. And make sure to respect the other arguments too to get a correctly styled output.

An advanced example: It returns nothing if there is no menu assigned to the theme location – and a link to the menu editor if the current user can actually create a menu:

 * Menu fallback. Link to the menu editor if that is useful.
 * @param  array $args
 * @return string
function link_to_menu_editor( $args )
    if ( ! current_user_can( 'manage_options' ) )

    // see wp-includes/nav-menu-template.php for available arguments
    extract( $args );

    $link = $link_before
        . '<a href="' .admin_url( 'nav-menus.php' ) . '">' . $before . 'Add a menu' . $after . '</a>'
        . $link_after;

    // We have a list
    if ( FALSE !== stripos( $items_wrap, '<ul' )
        or FALSE !== stripos( $items_wrap, '<ol' )
        $link = "<li>$link</li>";

    $output = sprintf( $items_wrap, $menu_id, $menu_class, $link );
    if ( ! empty ( $container ) )
        $output  = "<$container class='$container_class' id='$container_id'>$output</$container>";

    if ( $echo )
        echo $output;

    return $output;

Now we can call this menu like this …

$menu = wp_nav_menu(
        'theme_location' => 'primary',
        'fallback_cb'    => 'link_to_menu_editor',
        'echo'           => FALSE

echo $menu;

… and an admin will get a useful link within all the markup the theme’s stylesheet is expecting:

enter image description here

A regular visitor will get nothing.

  • 1
    Neat little function here - very useful Oct 5, 2013 at 0:15
  • the menu with the lowest ID is shown only if menu (but not theme_location) is passed and menu does not match any menu.
    – sam
    Jul 9, 2014 at 19:03

See wp_nav_menu() in the Codex

The default fall-back (the fallback_cb parameter) for wp_nav_menu is 'wp_page_menu' so I think you should auto-magically have a page menu (a menu with every static page and only static pages) if you do not assign a primary menu.

Is this not the case?

  • I see.... So I guess I should just use this 'fallback_cb' => 'wp_page_menu',
    – MartyBoi
    Sep 7, 2012 at 23:50
  • 2
    you shouldn't have to. that is the default.. you actually have to pass a null string to get rid of it. are you not getting a menu? do you have any pages in your blog? Sep 7, 2012 at 23:51
  • 1
    That's not true. You have to unset it using false. Passing a null will simply leave the original fallback in.
    – kaiser
    Sep 8, 2012 at 0:14
  • 1
    You are correct. We're still waiting to hear if @MartyBoi is not seeing anything as a fallback, because it should already be happening by default right? Sep 8, 2012 at 1:27
  • Yes... right :)
    – kaiser
    Sep 8, 2012 at 6:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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