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.
The parameter
fallback_cb
will be used when the user has no menu assigned to yourtheme_location
. It should be a function that returns a string, but you can use__return_false()
to suppress any output:You should always declare the fallback explicitly to keep your code readable.
Take the example from TwentyEleven:
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:
Now we can call this menu like this â¦
⦠and an admin will get a useful link within all the markup the themeâs stylesheet is expecting:
A regular visitor will get nothing.
See
wp_nav_menu()
in the CodexThe default fall-back (the
fallback_cb
parameter) forwp_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?