0

I am new to wordpress, and i am developing a custom theme, while i have registered a navigation menu in the theme. But the menu is showing automatically on the page while there are no items in the menu i have created from wordpress admin panel.

register_nav_menu( 'headerLocationMenu', 'Header Menu Location' ); // functions.php

wp_nav_menu( array(
                        'theme_location' => 'headerMenuLocation',
                        'container' => 'ul',
                        'menu_class' => 'navbar-nav ml-auto',
                    )); // template file code

Am I doing anything wrong?

Thanks in advance.

2 Answers2

0

What do you mean it's showing automatically? You have to have defined location somewhere in your template where the menu will live.

It may be possible that WP just added all of your pages (alphabetically) to the menu, until you populate the menu. Did you try adding pages or any other links to your menu? And then checking if you get the menu you expected on your page.

EDIT

Why don't you try it like this:

First delete code above from your functions.php file. and instead simply add this:

if (function_exists('add_theme_support')) {
    add_theme_support('menus');
}

And then instead of making "menu locations" in your files just add this where you want your menu to appear:

<?php wp_nav_menu(array( 'menu' => 'NAME OF YOUR MENU' )); ?>
idoric
  • 179
  • 6
  • Yes, I have defined the location and created the new menu from admin panel, but I have not added any links or pages as menu item in the menu. But still my menu on front end shows all the pages in the navigation. and still if add the any links and pages to the menu, the menu items get ordered alphabetically though. – Vyankatesh Charakpalli Mar 13 '18 at 09:42
  • @VyankateshCharakpalli ok so I edited my answer. Can you try that out. – idoric Mar 13 '18 at 10:29
0

This happens because the theme_location value doesn't match the location registered using the register_nav_menu function (i.e. you have a typo).

In your example, you spell it like headerLocationMenu when registering it, but later you spell it like headerMenuLocation when calling the menu in your template files.

So just fix the typos and it will work :)

Jesse Nickles
  • 1,435
  • 1
  • 17
  • 25