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

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

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

And if you could, please respond with an example.

share|improve this question
add comment

2 Answers

Take a look at this link.

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?

share|improve this answer
    
I see.... So I guess I should just use this 'fallback_cb' => 'wp_page_menu', –  MartyBoi Sep 7 '12 at 23:50
    
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? –  helgatheviking Sep 7 '12 at 23:51
    
That's not true. You have to unset it using false. Passing a null will simply leave the original fallback in. –  kaiser Sep 8 '12 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? –  helgatheviking Sep 8 '12 at 1:27
    
Yes... right :) –  kaiser Sep 8 '12 at 6:53
add comment

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:

wp_nav_menu(
    array(
        '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:

<?php 
/* 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' ) )
    {
        return;
    }

    // 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(
    array(
        '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.

share|improve this answer
    
Neat little function here - very useful –  Devin Walker Oct 5 '13 at 0:15
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.