This PHP code enables you to change the primary nav menu for any permalink slug or i.d.
1. primary – This is the Theme Location the default menu is assigned to. Go to Appearance > Menus > Manage Locations to configure this setting.
2. default – This is the name of the menu. Go to Appearance > Menus > Edit Menus to create a menu named default.
3. about – This is the permalink slug for the page named About. You can use the slug for any other page or the post/page/tag slug in its place. You can also change the conditional tag to any other.
4. custom – This is the name of the conditional menu which you can create. Go to Appearance > Menus > Edit Menus to create a menu named custom. When you click on the menu item named about, the menu with the name custom will display rather than the menu named default.
The code also includes the PHP comparison operator == and the basic assignment operator = which is used to write a value to a variable.
Hope you find this code and tutorial useful and and look forward to any questions you may have about this topic in the comments below.
Hi Brad,
Thanks so much for all your snippets and tutorials; I use them every time. This argument works for the primary navigation location. What if I want to do same for the secondary navigation?
Change primary to secondary in the code.
Just wanted to say thank you! Worked great. Even tweaked it with some “if in_category” stuff to show on posts. Was a great find.
Thanks again.
Hi this is great works perfectly – I am trying to add a second set of arguments to display another menu for a new set of pages on a regional site – can you help. I tried this but getting errors – first part works fine for au – how do I add a second array of pages
Hi Sam
Please paste PHP code in a Github Gist and link to it from here. Thanks
This will come in handy! Thanks Brad. I use your snippets all the time.
You’re welcome Herb.
More on this topic coming soon.