Home > Back-end >  Difference of menu and theme_location
Difference of menu and theme_location

Time:06-07

I'm trying to create a menu by wp_nav_menu(), there are two argument seems the same although the name are different: menu and theme_location. Assume I already register a menu and the name/id is nice, now I want to display the menu on a page by wp_nav_menu(), this is what I do:

wp_nav_menu( array(
   'menu' => 'nice',
   'theme_location' => 'nice'
) );

If I don't specify the theme_location, it will still work? Because I already tell wordpress the menu I wanna display, and vice versa, if I specify the theme location, why do I still need to specify menu which I wanna use?

CodePudding user response:

menu and theme_location has some explanations, menu is for specific menu id, slug, name or object where you will show that specific menu by the menu id, slug, name or object, on the other hand theme_location is where you want to show your menu. It is not necessary to set the menu and theme_location at a time, but if you set two options at a time it will priority the menu first then theme_location, in case you have no menu with that name/slug then it will check the theme_location, in case you set a wrong theme_location which is actually not registered then it will call the fallback_cb. Note that menu name comes from the admin panel https://prnt.sc/j-m6B-jY252k where theme_location comes from the functions.php file where you registered the menu https://prnt.sc/BglTqU_xows2 . This theme_location will show in the menu area in the admin panel https://prnt.sc/KY7cYottkJWe . So, in conclusion, you can use any one of it for showing the menu but it is totally up to you which way you will choose, but for developers it is wise to set theme_location instead of menu. I have tried to explain everything from my end but if you have more questions you can read the official details of menu here

  • Related