3

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?

2
  • 1
    What happens if you use a different hook like init? You also haven't added a container class to the code. Commented Feb 23, 2014 at 10:44
  • I second what Brad is saying, change add_action( 'after_setup_theme', 'my_cool_menu_function' ); to add_action( 'init', 'my_cool_menu_function' );
    – ssergei
    Commented Nov 12, 2014 at 1:39

3 Answers 3

7

Put this pretty much anywhere (ie. functions.php)

add_theme_support( 'menus' );
2
  • 1
    In WordPress 5.3 menus is not a feature listed in add_theme_support() so this code wouldn't have any effect. Commented Mar 15, 2020 at 18:47
  • In 5.3.2 its showing up for me when I do this. I noticed it stopped showing a while ago on newer WP versions, maybe they reverted it.
    – Joel M
    Commented Mar 16, 2020 at 0:48
0

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

5
  • 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
    Commented Feb 22, 2014 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) Commented Feb 22, 2014 at 22:39
  • No this answer is not correct and has nothing to do with the question.
    – Joel M
    Commented Mar 15, 2020 at 3:17
  • @helgatheviking, it probably works for you because you are on an older version or your theme adds the support for menu's elsewhere.
    – Joel M
    Commented Mar 15, 2020 at 3:17
  • Not sure if that was the case 6 years ago, but you don't have to declare support for menus as they have been automatically supported for a long time. OP said this >I can able to see few links in frontend which are pages i guess. Which is what made me think they were getting wp_page_menu() via the default fallback_cb paramter of wp_nav_menu. Which may not have been the OP's problem, but was a very relevant thing to check, especially considering their register_nav_menu() code is correct. Commented Mar 15, 2020 at 18:52
0

Try with init

add_action( 'init', 'my_cool_menu_function' );

I did it, and it works for me.

Cheers.

Reference: https://codex.wordpress.org/Navigation_Menus

Your Answer

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

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