-
Ben99
hello,
is it possible to create 2 different navigations where they appear on different pages on desktop and mobile?
There are 2 different locations on the website and they each have a different menu.
I would be very grateful for your help -
Yes, you can create 2 different menus, and get their ID which can be found in the URL when you edit the menu.
Here’s the PHP code which activates ID 10 menu on ID 100 page, and ID 20 menu on ID 200 page, you need to switch the menu IDs and page IDs to the actual ones.
add_filter( 'wp_nav_menu_args', function ( $args ) { if ( 'primary' === $args['theme_location'] && is_page( 100 ) ){ $args['menu'] = 10; } if ( 'primary' === $args['theme_location'] && is_page( 200 ) ){ $args['menu'] = 20; } return $args; } );
-
Ben99
great thanks and it works.
Should I better not set either of the two navigations as primary? And another question about the spelling:
i want to add several pages to a menu, unfortunately the spelling is not correct, if you could still show me the correct one here! Many thanks in advance?add_filter( ‘wp_nav_menu_args’, function ( $args ) {
if ( ‘primary’ === $args[‘theme_location’] && is_page( 100 ) ){
$args[‘menu’] = 2;
}
if ( ‘primary’ === $args[‘theme_location’] && is_page( 1366, 1477 ) ){
$args[‘menu’] = 7;
}return $args;
} ); -
David
Hi there,
the best option would be to choose one menu as the default menu and set its Location to the Primary Menu.
Then just add a function to replace that menu where you require it eg.
add_filter( 'wp_nav_menu_args', function ( $args ) { if ( 'primary' === $args['theme_location'] && is_page( 1366, 1477 ) ){ $args['menu'] = 7; } return $args; } );
-
Ben99
thank you and it works perfect!
-
David
glad to hear that !
- You must be logged in to reply to this topic.