I did this by following steps:
Step 1: First creating a default primary menu. Navigate to Appearance > Menus and create a default primary menu for your site. Check the “Primary Navigation Menu” checkbox and save this menu.
Step 2: Creating another menu for pages only. Navigate to Appearance > Menus and create another menu for your pages.
Step 3: Now add this simple code in functions.php file
1 2 3 4 5 6 7 8 9 10 |
function gd_nav_menu_args( $args ){ if( ( 'primary' == $args['theme_location'] ) && is_singular('page') ) { $args['menu'] = 'Menu for Page'; // Add your menu name here. My case it is "Menu for Page" } return $args; } add_filter( 'wp_nav_menu_args', 'gd_nav_menu_args' ); |
is_singular('page')
conditional tag will return true when user is visiting a page. You can change this conditional tag as per your requirement.
Eva says
Hi, thank you for this tutorial! I am just having a little problem with the implementation. I want the menu for the home pages (with the anchors) on the HOME only. I made another menu that I use in the footer, and I would like to use this on the pages. It doesn’t seem to work, so I would like to know if I should have the menus point to a certain location, eg the Header Menu and the Footer Menu. I don’t want to use the Before Header Menu.
Hope you can help me out. Thank a lot!!
Eva
Paul says
If you share the wp-admin access via contact page, then I can help you.
Ramya says
I need to display the current user’s name in secondary menu. any help?
Krystyn says
If I only want a certain menu to display on the home page, would I use? I’m using it, but it doesn’t seem to be switching the menu on the home page.
function gd_nav_menu_args( $args ){
if( ( ‘primary’ == $args[‘theme_location’] ) && is_home() )
{
$args[‘menu’] = ‘Landing Page Menu’; // Add your menu name here. My case it is “Menu for Page”
}
return $args;
}
add_filter( ‘wp_nav_menu_args’, ‘gd_nav_menu_args’ );
Paul says
Is ‘Landing Page Menu’ your menu name? Double check it.
Also if you are using the static page as a front page then you will change
is_home()
withis_front_page()
.Greg Welch says
Thank you , for the sweet trick
worked great after adding “post” into each “is” statement
if( ( ‘primary’ == $args[‘theme_location’] ) && ( is_archive(‘post’) || is_singular(‘post’) ) )
{
Greg Welch says
Sorry also forgot to mention that i kept: || is_singular(‘page’)
Sweet
Greg Welch says
Really Cool function,
hoping you could add how i might use for both archive and post function above
am i close?
function gd_nav_menu_args( $args ){
if( ( ‘primary’ == $args[‘theme_location’] ) && is_archive(‘post’) )
if( ( ‘primary’ == $args[‘theme_location’] ) && is_singular(‘page’) )
{
{
$args[‘menu’] = ‘Menu for Post’; // Add your menu name here. My case it is “Menu for Page”
}
return $args;
}
add_filter( ‘wp_nav_menu_args’, ‘gd_nav_menu_args’ );
Paul says
I think that this is wrong
]
You should try this
]
If you have custom post type then you will try is_post_type_archive() function