Two Navigation Menus in Themes Produce the same menus?

All we need is an easy explanation of the problem, so here it is.

Alright so I’ve got two navigation menus that I’ve registered with my theme:

<?php

if ( function_exists('register_sidebar') )
    register_sidebar();

add_theme_support( 'menus' );

if ( function_exists('register_nav_menu') ) {
    register_nav_menu( 'primary_nav', 'Primary Navigation');
    register_nav_menu( 'secondary_nav', 'Secondary Navigation');
}

?>

Functions.php



And then Beyond that I’ve got two places in my header where I display the navigation menus:

                <!-- BEGIN: Navigation Menu primary_nav (Primary Navigation) -->
                <?php
                  if(function_exists('wp_nav_menu'))
                  {
                    wp_nav_menu(
                        array(
                            'menu' => 'primary_nav',
                            'container' => '',
                            'depth' => 1,
                            'menu_id' => 'menu'
                        )
                    );
                  }
                  else 
                  {
                ?>
                   <ul>
                    <?php wp_list_pages('title_li=&depth=1'); ?>
                   </ul>
                <?php
                  }
                ?>
                <!-- END: Navigation Menu primary_nav (Primary Navigation) -->
            </div>

header.php – Location 1

        <!-- BEGIN: Secondary Navigation Menu -->
        <?php 
              if(function_exists('wp_nav_menu')) {
                wp_nav_menu(
                    array(
                        'menu' => 'secondary_nav',
                        'container' => '',
                        'depth' => 1,
                        'menu_id' => 'secondary_nav_id'
                    )
                );
              }
              else {
        ?>

            <ul>
                <?php wp_list_pages('title_li=&depth=1'); ?>
            </ul>
        <?php
              }
        ?>
        <!-- END: Secondary Navigation Menu -->

header.php – Location 2

In the admin page for the theme everything shows up just fine, both menus appear in the list both Primary Navigation and Secondary Navigation.

And from the admin screen I’ve added two distinct sets of menu items. However, whenever I load up my theme, for some reason I end up with only the menus from the Primary Navigation appearing twice.

Is there something I’ve forgotten that would cause this behavior?

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

Try theme_location instead of menu in arguments. Too late here to look up specifics, but I remember documentation/code being absolutely confusing about that.

Method 2

In addition to Rarst’s and Chip’s answers above, register_nav_menu is used in this in manner:
register_nav_menu( $location, $description )

What you’ve specified as primary_nav and secondary_nav are actually menu location identifiers and not the actual menu identifiers.

That is why you have to use theme_location in your wp_nav_menu arguments instead of menu

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply