1

I'm trying to change the menu in a primary and a secondary menu depending on the page which is visited.

My primary menu is in the top of the page, and it looks like this:


Index | About | Contact


And my second menu is a dropdown menu, under the header, and it looks like this:


Write New | Help | Profile |


So, the thing is that whenever you visit profile for example, I want to change both menus for others I've already created.

So reading I found something but it doesn't work at all: in the file functions.php I added:

add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' );

function my_wp_nav_menu_args( $args) {
if( is_page('2') ) {
        $args['menu'] = 'Original Menu';
} else {
        $args['menu'] = 'Menu for Profile';
}
    return $args;
}

It works, but it changes primary and secondary menus with the 'Menu for Profile', and I want it only for the primary. I've tried adding ['theme_location'] = 'primary' but what it does is dissapear both menus.

I hope you can help me, and thanks.

0

You most probably used the theme location part wrongly. Here is an example of how it should be done

Follow the comments in code for explanation :-)

add_filter( 'wp_nav_menu_args', function ( $args ) 
{
    if (    is_page( 2 ) //Only target page 2 
         && $args['theme_location'] === 'primary' ) { // Check and only target the primary menu
    ) {
        $args['menu'] = 'Menu for Profile';
    }
    return $args;
});  
-2

You have to create two menus and call them with a conditional in header.php file, like this:

if ( is_page( 'about' ) ) { 
    wp_nav_menu( array(
        'menu'           => 'primary', 
        'theme_location' => 'primary'
    ) ); 
} else { 
    wp_nav_menu( array(
        'menu'           => 'second_menu', 
        'theme_location' => 'primary'
    ) );
}
  • 1
    Please reread the question. – Pieter Goosen Nov 17 '15 at 7:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.