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?
Put this pretty much anywhere (ie. functions.php)
The default fallback for
wp_nav_menu()
iswp_page_menu()
. So if you have not set a menu for a specific location and since you aren’t changing thefallback_cb
parameter in your header.php code, then you will see a list of pages. Additionally yourvar_dump
will return an empty array. You need to go to Appearance>Menus and create a menu. Then assign it to the primary location.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