Support » Developing with WordPress » Added secondary menu, it replaced the primary

  • Resolved vovazuev

    (@vovazuev)


    Hi!
    I’m working with the blank theme. I have the primary menu (made of pages) and need to add the secondary menu on one page. First I registered an additional menu in functions.php. Then I created the secondary menu and assigned it to the secondary display location. Then I went to the specific page (page-{slug}.php) and added the following:

    <?php wp_nav_menu(array(
        'theme-location' => 'additional-menu',
        'menu_id' => 'additional-menu',
        'menu_class' => 'nav nav-pills mt-3',
        'container' => false
         ));
     ?>

    My header.php contains the following:

    <?php wp_nav_menu(array(
         'theme-location' => 'primary',
         'menu_id' => 'primary-menu',
         'menu_class' => 'nav nav-tabs mt-5',
         'container' => false
          ));
     ?>

    The both menus assigned to proper theme locations.

    But after I saved the new/secondary menu, the primary menu disappeared from the layout, replaced by the secondary menu.

    When I delete the secondary menu, the primary menu returns.

    Where is my error?
    Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    And you’ve verified the “Manage Locations” tab of the menu admin screen has both locations and a different menu is assigned to each, I assume? If so, all I can imagine is there is a conflict from plugins or from within other code of your own theme. Deactivate all plugins to rule out plugin conflicts.

    Try using different location tags to remove a possible name collision. “primary” is a common tag name which could be in conflict with other code. Menu issues can be tricky to troubleshoot because WP will put up any other menu it can find if it cannot find the requested one. If all else fails it’ll make up its own from pages. This can confuse what you see output as menus.

    If all else fails, you should do a focused debug trace through wp_nav_menu() to figure out why it is getting the menu data it gets. This involves altering core code in order to output debug data. This is OK as long as the original file is restored afterwards.

    Thanks bcworkz!

    I don’t have plugins installed that may cause troubles with menus. I tried to use different location tags, it didn’t help. Debugging that you’ve suggested was way over my skills.

    In desperation I activated another theme (20-19) and noticed that it displayed my menus correctly. I went to its code and only took from there this conditional:

    <?php if ( has_nav_menu( 'menu-1' ) ) : ?>
      <!-- my wp_nav_menu function body -->
    <?php endif; ?>

    And similar construction for the secondary menu.

    After that I saw my menus in place.

    But when I removed these conditionals for an experiment, my menus continued to show properly. I tried to recreate my error but without success.

    The issue has been resolved but I don’t know exactly how…

    • This reply was modified 3 months, 4 weeks ago by  vovazuev.
Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.