0

I just noticed something weird I never saw before. I'm working on a project using WP 3.6.1. This project that has two different menus (actually, three, but that's not important). I'm calling them through the wp_nav_menu function.

I've done this kind of thing quite a few times now... but today is different. Today it doesn't work :(

My PHP code, placed in header.php, looks like this:

        <nav id="nav-menu-container">
            <div id="main-menu" class="main-menu menu clearfix">
                <?php wp_nav_menu('header_main'); ?>
            </div>
            <div id="secondary-menu" class="secondary-menu menu clearfix">
                <?php wp_nav_menu('header_secondary'); ?>
            </div>
        </nav>

The menu items are declared in functions.php and two different Menus are configured through the dashboard to show up in each location.

    if (function_exists('register_nav_menus')){
        register_nav_menu('header_main', 'Menú principal' );
        register_nav_menu('header_secondary', 'Menú secundario' );
    }

But the first menu keeps on being printed in the two locations. As I said above, using two different menu is something that Ive done in the past, but today I don't see the error... and there is and error, as it is not working :S

Any ideas?

2 Answers 2

3

You're not calling wp_nav_menu() correctly.

You need to pass it an args array, and declare the theme_location key.

I'm assuming that, since you're passing a string rather than an array, wp_nav_menu() doesn't recognize the parameter, and is falling back to its default output:

  • the menu matching the ID, slug, or name given by the menu parameter, if that menu has at least 1 item;
  • otherwise, the first non-empty menu;
  • otherwise, output of the function given by the fallback_cb parameter (wp_page_menu(), by default);
  • otherwise nothing.

And you're seeing the first non-empty menu output in both locations.

Try this instead:

wp_nav_menu( array(
    'theme_location' => 'header_main'
) ):

and

wp_nav_menu( array(
    'theme_location' => 'header_secondary'
) ):
1
  • thanks, I was adding an answer, i found the problem :) thanks @chip
    – versvs
    Oct 2, 2013 at 16:33
0

The problem is that wp_nav_menu requires as input an associative array and i'm entering a raw string.

This code may work everywhere.

    <nav id="nav-menu-container">
        <div id="main-menu" class="main-menu menu clearfix">
    <?php wp_nav_menu( array('theme_location' => 'header_main') ); ?>
        </div>
        <div id="secondary-menu" class="secondary-menu menu clearfix">
            <?php wp_nav_menu( array('theme_location' => 'header_secondary') ); ?>
        </div>
    </nav>

Sorry for the double post. It seems that the first menu was not appearing either, rather it was shown as a default option as no value was being passed at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.