I’ve created a menu using wp_nav_menu in my theme using the most simple setup as possible:
wp_nav_menu( array( 'theme_location' => 'my_theme_top_menu') );
And in my functions.php:
register_nav_menus(
array('my_theme_top_menu' => 'Top Menu' )
);
However, I’m running into strange behaviour when the user creates a menu in wp-admin and assigns it to this menu. When you first select the theme, it defaults to the default menu built from pages, the markup looks like this:
<div class="menu">
<ul>
<li class="page_item page-item-51">
<a href="http://www.mdunham.co.uk/bligs/">About Us</a>
But, if a user selects one of their pre-defined menus the markup that builds the menu completely changes:
<div class="menu-main-menu-container">
<ul id="menu-main-menu" class="menu">
<li id="menu-item-102" class="menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-29 current_page_item menu-item-102">
<a href="http://www.site.co.uk/">Home</a></li>
Why is this happening? It’s like when uses a defined menu WP menu it WP defaults(or settings I’ve defined), yet when there is no defined menu and it falls back it uses a different, bastardized set of options which I haven’t actually set anywhere myself.
Here’s the default usage of
wp_nav_menu()
:Notice that the default fallback for
wp_nav_menu()
is wp_page_menu(), for which the defaults are:These should produce mostly the same output, with the same set of pages. However, if you need more specific control, you have a couple options:
Assign a custom callback here:
‘fallback_cb’ => ‘mytheme_wp_nav_menu_cb’
…and then, define:
…so that you override
wp_page_menu()
Wrap your
wp_nav_menu()
call in ahas_nav_menu()
conditional:…in the
else
statement, you can do whatever.