This is the first paragraph of the documentation of the function wp nav menu:

Given a theme_location parameter, the function displays the menu assigned to that location, or nothing if no such location exists or no menu is assigned to it.

However, I am providing a theme_location that I have not registered, and in spite of that a menu item appears.

Here's my functions.php:

function register_my_menu() {
    register_nav_menu('header-menu',__( 'Header Menu' ));
add_action( 'init', 'register_my_menu' ); ?>

And here's how I call the menu:

<?php wp_nav_menu($args = array('theme_location' => 'nonsense')); ?>

On my page, a menu with the default item "Sample page" appears. According to the documentation I cite, nothing should be displayed, because the location "nonsense" should not exist.

Is the documentation wrong, or am I making some mistake?


The documentation is wrong. Check the source. There are two telling comments. First there is:

// Get the nav menu based on the theme_location

And after which there is another comment that reads:

// get the first menu that has items if we still can't find a menu

Following that last comment the code runs wp_get_nav_menus and loops through every menu on the site hoping to find one that has associated items. In other words, wp_nav_menu tries desperately to create a menu and will use the first one it finds. I don't understand the logic behind that code. I get the concept of fallbacks, but this strikes me as fallback overkill.

You will need to wrap your code in has_nav_menu to prevent this behavior.

if (has_nav_menu('nonsense')) {
  wp_nav_menu(array('theme_location' => 'nonsense'));
  • 2
    In case you want to head/desk a bit more: If no location is defined, you'll get back all nav menu item/WP_Post objects no matter what. And yes, it's callback behind callback, senseless amount of func_get_args() aligning normal arguments in the method heads, etc. Oh ... and all nicely wrapped up in PHP 4 code. facepalm ... Did I mention that the nav menu is WooThemes creation (a.k.a.: fault)? – kaiser Jan 25 '14 at 20:07
  • 1
    @kaiser: I didn't know that about Wootheme, but a lot of things make more sense now – s_ha_dum Jan 25 '14 at 20:31

The documentation was wrong and inconsistent. There is the additional parameter fallback_cb which determines what happens if the menu_location does not exist. By default, it calls wp_page_menu which displays some default menu.

I have corrected the documentation, so this question has been settled.

Thanks to s_ha_dum for pointing me into the right direction!

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.