Change Nav Menu For Specific Page In Any Theme

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.

Similar Posts


Comments

7 responses to “Change Nav Menu For Specific Page In Any Theme”

  1. Carlson007 Avatar

    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?

    1. Brad Dalton Avatar
      Brad Dalton

      Change primary to secondary in the code.

  2. Joe Young Avatar
    Joe Young

    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.

  3. 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

    1. Brad Dalton Avatar
      Brad Dalton

      Hi Sam

      Please paste PHP code in a Github Gist and link to it from here. Thanks

  4. Herb Trevathan Avatar
    Herb Trevathan

    This will come in handy! Thanks Brad. I use your snippets all the time.

    1. Brad Dalton Avatar
      Brad Dalton

      You’re welcome Herb.
      More on this topic coming soon.

Leave a Reply

Join 5000+ Followers

Get The Latest Free & Premium Tutorials Delivered The Second They’re Published.