fall back for main menu?

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?

Read More

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.

Related posts

Leave a Reply

2 comments

  1. 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.

  2. 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?