Languages switcher

Posted in: Newspaper
Post count: 95

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.

Post count: 95

Please…

Post count: 20685

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

Post count: 95

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!

Post count: 20685

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;
}

Post count: 95

It seems work… thanks!

Viewing 6 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic.