Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I tried few tutorials. All i think is i should use register_nav_menus() and then add the menu in header.php

Here is the code i tried in functions.php from this help site itself

function my_cool_menu_function(){
  register_nav_menus( array(
    'primary' => 'Primary Navigation'
  ));
}

add_action( 'after_setup_theme', 'my_cool_menu_function' );

In header.php

<?php wp_nav_menu( array( 'theme_location' => 'primary' ) ); ?>

When i tried to check whether any menu is registered with this function $locations = get_nav_menu_locations();var_dump($locations);

I got array of size zero. i,e no menu registered.

Am i missing something?

I can able to see few links in frontend which are pages i guess.

I see that Your theme does not natively support menus, but you can use them in sidebars by adding a “Custom Menu” widget on the Widgets screen.

So i think above menu is not registered.

What wrong am doing here?

I have added add_theme_support('nav-menus'); too in functions.php

Updates

I tried the same above code through plugin then i can able to see the menu. Which mean it is not working with the theme functions.php file. Am not sure why this happens.

Do anyone know this?

share|improve this question
    
What happens if you use a different hook like init? You also haven't added a container class to the code. –  Brad Dalton Feb 23 at 10:44

1 Answer 1

The default fallback for wp_nav_menu() is wp_page_menu(). So if you have not set a menu for a specific location and since you aren't changing the fallback_cb parameter in your header.php code, then you will see a list of pages. Additionally your var_dump will return an empty array. You need to go to Appearance>Menus and create a menu. Then assign it to the primary location.

enter image description here

share|improve this answer
    
Thanks for the answer +1. But that is not the issue now i think the menu itself not supported in theme. Your theme does not natively support menus, but you can use them in sidebars by adding a “Custom Menu” widget on the Widgets screen. –  rram Feb 22 at 22:34
1  
But your theme does support menus. I've tested your code and can output a menu, so there must be some other conflict or weird coding going on in your theme. Maybe check header.php for strange conditionals. Run a search through all your theme files for the error message. Are you sure the functions.php that you are editing is the active theme? (Yes I had to ask and have made that error myself) –  helgatheviking Feb 22 at 22:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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