I want to use different menus in the position "main menu" for different pages.
For example:
I create:
- 1 Main menu called "Main"
- 1 Main manu called "Sales page"
- 1 Page called "Home"
- 1 Page called "Product"
And I want to use:
- the menu called "Main" in page called "Home"
- and the menu called "Sales page" in the page called "Product"
Thank you very much.
Elisabet
Joshua Nelson answers:
If you have a theme you feel like editing you can change where the menu is referenced to something like this:
if ( is_page( 'home' ) && is_nav_menu( 'Main' ) ) { // home page & make sure menu exists
wp_nav_menu( array( 'menu' => 'Main' ) ); // main menu
} elseif ( is_page( 'product' ) && is_nav_menu( 'Sales Page' ) ) { // product page & make sure menu exists
wp_nav_menu( array( 'menu' => 'Sales Page' ) ); // sales page menu
} elseif ( is_nav_menu( 'Main' ) ) { // if not those pages, then make use this menu
wp_nav_menu ( array( 'menu' => 'Main' ) ); // default menu, just in case
} else {
wp_nav_menu(); // if all else fails, use wordpress defaults
}
Or you can find a plugin like this one: [[LINK href="http://wordpress.org/plugins/custom-page-menus/"]]http://wordpress.org/plugins/custom-page-menus/[[/LINK]] (don't know if it works still)
Joshua Nelson comments:
There are some really good tips in this post too: [[LINK href="http://justintadlock.com/archives/2010/06/01/goodbye-headaches-hello-menus"]]http://justintadlock.com/archives/2010/06/01/goodbye-headaches-hello-menus[[/LINK]]
Abdelhadi Touil answers:
Hi.
In my opinion Joshua Nelson's answer is the best solution for you. In your situation the code should be:
<?php
if ( is_page( 'Home' ) ) {
wp_nav_menu( array( 'menu' => 'Main' ) );
} elseif ( is_page( 'Product' ) ) {
wp_nav_menu( array( 'menu' => 'Sales page' ) );
} else {
wp_nav_menu ( array( 'menu' => 'main menu' ) ); // default menu
}
?>
Good luck!
Sébastien | French WordpressDesigner answers:
The simplest solution is probably to use a template for each page.
Here you can learn how to create a template for "products" and another template for "home" : [[LINK href="http://codex.wordpress.org/Page_Templates#Custom_Page_Template"]]http://codex.wordpress.org/Page_Templates#Custom_Page_Template[[/LINK]].
It's very very simple. (tell me if you don't understand)
And in each template, use the code to insert the menu you want to display.
So in the template of home you will use a code like this :
wp_nav_menu( array( 'menu' => 'Main' ) )
and in the template of products you will use a code like this :
wp_nav_menu( array( 'menu' => 'Sales page' ) )
Sabby Sam answers:
All above answer are prefect. Even you could try this one.
<?php
if ( is_page(1) ) { // if ( is_front_page() ) { you can try this one too
wp_nav_menu( array( 'menu' => 'Main' ) );
} elseif ( is_page( 5) ) {
wp_nav_menu( array( 'menu' => 'Sales page' ) );
} else {
wp_nav_menu ( array( 'menu' => 'main menu' ) ); // default menu
}
?>
For more reference visit this link
http://codex.wordpress.org/Function_Reference/is_page
tong chen answers:
step 1, add a post meta with page, add the following code to the functions.php
add_action( 'add_meta_boxes', 'ashu_add_custom_box' );
function ashu_add_custom_box(){
if ( function_exists('add_meta_box') ) {
add_meta_box( 'page_custom_menu','page-menu', 'page_custom_menu_box', 'page', 'side','high');
}
}
function page_custom_menu_box(){
global $post;
if ( metadata_exists( 'post', $post->ID, 'page_menu' ) ) {
$menu_id = get_post_meta( $post->ID, 'page_menu', true );
}
$entries = get_terms( 'nav_menu', array( 'hide_empty' => false ) );
echo '<select class="postform" id="page_menu" name="page_menu"> ';
echo '<option value="">Select...</option> ';
foreach ($entries as $key => $entry){
$id = $entry->term_id;
$title = $entry->name;
if ( $id == $menu_id ){
$selected = "selected='selected'";
}else{
$selected = "";
}
echo"<option $selected value='". $id."'>". $title."</option>";
}
echo '</select>';
}
add_action('save_post', 'save_postdata');
function save_postdata( $post_id ) {
global $post;
if( !isset($_POST['page_menu']) )
return;
$data = $_POST['page_menu'];
if(get_post_meta($post_id, 'page_menu') == "") {
add_post_meta($post_id, 'page_menu', $data, true);
}elseif($data != get_post_meta($post_id, 'page_menu', true)) {
update_post_meta($post_id, 'page_menu', $data);
}elseif($data == "") {
delete_post_meta($post_id, 'page_menu', get_post_meta($post_id, 'page_menu', true));
}
}
step 2,change where the menu is referenced:
<?php
if( is_page() && get_post_meta( $post->ID, 'page_menu', true )!='' ){
global $post;
$menu_id = get_post_meta( $post->ID, 'page_menu', true );
wp_nav_menu( array( 'container_class' => 'menu-header', 'menu' => $menu_id ) );
}else{
//default menu
wp_nav_menu( array( 'container_class' => 'menu-header', 'theme_location' => 'primary' ) );
}
?>
step 3 ,select a custom menu with each page