Hi!
I have:
– Header Menu named “Italian”
– a second menu named “English”
– a third menu as Top Header Menu named “Languages”
I’m using this code add_filter('wp_nav_menu_args', function ($args) {
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (strpos($url,'/en/') !== false) {
$args['menu'] = 'English';
}
return $args;
});
The problem is that when I switch to a URL that contains /en/ I see the “English” menu as Header Menu but also Top Header Menu will be replaced with the “English” menu! When I switch to a URL that contains /en/ Top Header Menu named “Languages” must be present.
Hi,
So you want to switch the menu depending on URL. Maybe try a plugin that can switch menus, or search for more code solutions online. I have never tried something like this unfortunately. Maybe some other users can pitch in.
Also I can’t seem to find topics about this subject, not quite popular
– https://stackoverflow.com/questions/35347196/wordpress-load-different-menu-based-on-url
Yes Simion C., switch the menu depending on URL.
This code is OK add_filter('wp_nav_menu_args', function ($args) {
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (strpos($url,'/en/') !== false) {
$args['menu'] = 'English';
}
return $args;
});
But also Top Header Menu change!
Please, do a try on your WordPress installation to see why also Top Header Menu change!
Try it like this, should only change the main header menu
add_filter( 'wp_nav_menu_args', 'menu_switcher' );
function menu_switcher( $args ){
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (strpos($url,'/en/') !== false) {
if( 'header-menu' == $args['theme_location'] )
$args['menu'] = 'English';
}
return $args;
}
