How to add menu support to a theme?

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

Read More
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?

Related posts

3 comments

  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

Comments are closed.