Ask your WordPress questions! Pay money and get answers fast! (more info)

Different Menus for different pages

  • SOLVED

Hello, I would like to add a different menu (other than the main menu) on several pages of my site. My theme currently limited me to two menus (primary and footer). I've updated the function.php and header.php to include two more menus.

<?php wp_nav_menu( array('theme_location' => 'primary-menu','menu_id' => 'main-nav','menu_class' => 'sf-menu')); ?>
<?php wp_nav_menu( array('theme_location' => 'secondary-menu','menu_id' => 'nh-nav','menu_class' => 'sf-menu')); ?>
<?php wp_nav_menu( array('theme_location' => 'third-menu','menu_id' => 'ma-nav','menu_class' => 'sf-menu')); ?>

The problem is now when I create each menu and set locations, they display on all the pages. I need to know how to now include only the primary menu on specific pages, secondary menu on different pages and same for the third-menu. Ex below:

Primary menu to display on all domain.com/subfolder pages
secondary menu to display on all domain.com/nh pages
Third menu to display on all domain.com/ma pages

I've tried menu swapper plug in but when I tell menu swapper to switch primary to secondary, it displays secondary twice and third menu because it seems the code is calling all three menus to display.

Any help would be great!

Thank you,
Lauren

Answers (3)

2016-02-14

Bob answers:

domain.com/nh and domain.com/ma are parent page?

generally we can add if condition for particular page validation lik

if(is_page('nh')){
wp_nav_menu( array('theme_location' => 'third-menu','menu_id' => 'ma-nav','menu_class' => 'sf-menu'));
}elseif('ma'){
wp_nav_menu( array('theme_location' => 'secondary-menu','menu_id' => 'nh-nav','menu_class' => 'sf-menu'));
}else{
wp_nav_menu( array('theme_location' => 'primary-menu','menu_id' => 'main-nav','menu_class' => 'sf-menu'));
}


Bob comments:

We can also put condition based on url or string in url.

Ping me in skype at "thevaghela" if your are online right now.


Bob comments:

put this code in your functions.php file
This function will help to find current page url.

function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}


now we can check if a particular string is available in page url or not. and If it is then based on that we can show different menu.



if( strpos( $pageurl, '/nh/' ) ){
wp_nav_menu( array('theme_location' => 'third-menu','menu_id' => 'ma-nav','menu_class' => 'sf-menu'));

}elseif( strpos( $pageurl, '/ma/' ) ){

wp_nav_menu( array('theme_location' => 'secondary-menu','menu_id' => 'nh-nav','menu_class' => 'sf-menu'));

}else{

wp_nav_menu( array('theme_location' => 'primary-menu','menu_id' => 'main-nav','menu_class' => 'sf-menu'));

}


Lauren O'Neil comments:

Hi Bob, Thank you for your information. I would love to base the menu from it's url besides, I tried the original code you provided and it broke my site but I think that I am doing to wrong.

See below

<?php
if(is_page(array( 1249, 1337, 1332, 1235, 1236, 1232, 1233, 1234, 877, 369 )){
wp_nav_menu( array('theme_location' => 'secondary-menu','menu_id' => 'nh-nav','menu_class' => 'sf-menu'));
}elseif(is_page(array( 1142, 1335, 1336, 1333, 1334, 1145, 1237, 1238, 1149, 1146 )){
wp_nav_menu( array('theme_location' => 'third-menu','menu_id' => 'ma-nav','menu_class' => 'sf-menu'));
}else{
wp_nav_menu( array('theme_location' => primary-menu','menu_id' => 'main-nav','menu_class' => 'sf-menu'));
}
?>


Lauren O'Neil comments:

If I do the second url code option, where do I place the functions.php code? I skyped you as well.

Thank you,


Bob comments:

functions.php is file in your theme.

wp-content/themes/YOUR-THEME/functions.php


Bob comments:

the If condition code for header.php only. are you using same condition while registering menu in functions.php?

please show me your header.php and functions.php


Bob comments:

In url based solution I forgot to write line above if condition.

$pageurl =curPageURL();

so the full code should look like



$pageurl =curPageURL();

if( strpos( $pageurl, '/ma/' ) ){

wp_nav_menu( array('theme_location' => 'third-menu','menu_id' => 'ma-nav','menu_class' => 'sf-menu'));



}elseif( strpos( $pageurl, '/nh/' ) ){



wp_nav_menu( array('theme_location' => 'secondary-menu','menu_id' => 'nh-nav','menu_class' => 'sf-menu'));



}else{



wp_nav_menu( array('theme_location' => 'primary-menu','menu_id' => 'main-nav','menu_class' => 'sf-menu'));



}

2016-02-14

Navjot Singh answers:

One of these plugins might do the job - https://wordpress.org/plugins/if-menu/

Or

http://codecanyon.net/item/different-menu-in-different-pages/7857908

Or

https://themify.me/conditional-menus


Navjot Singh comments:

This question has also been answered here before with several good tips: http://wpquestions.com/question/show/id/8689


Lauren O'Neil comments:

I've tried the conditional menus plug in and it doesn't work. I'm hesistant to try any other plug ins especially paid because of the failures I've had with previous plug ins.

I was using page specific menu items plug in and it worked great...for desktop. It didn't render responsively so it displayed all my pages from mobile display.


Lauren O'Neil comments:

And Navjot, I did read through that post but I need to know how it could be applied with my code above. I don't want to lose my menu design and the example it provides is for one page. I have several pages that need to display the secondary menu, not just one.

2016-02-14

Andrea P answers:

hello!

there are a bunch of functions in wordpress which allows to target all pages of a specific type.
by instance:
all pages which are parent pages (i.e. they don't have a parent and eventually they have subpages)
all pages which are children of another page
all pages which are archives
all pages which are posts
all pages which are pages
all pages which are in one category or in one tag
etc...

are your domains actually specific wordpress pages/folders/archives/ etc?
or these are domain redirects/rewriting?

<blockquote>Primary menu to display on all domain.com/subfolder pages
secondary menu to display on all domain.com/nh pages
Third menu to display on all domain.com/ma pages </blockquote>

it looks like the last 2 of them are languages sub-domains. which plugin are you using for the languages? because it will likely to have a specific function to target all pages of a given language, so that's what we'd use for conditionally load the menus..