PolyLang/Menu change based on language
-
Hey,
I was wondering how I would go about changing the menu used based on the users language preference. I was thinking if I did it based on the url. The default language of the site is, say German, and the second language would be English.
Having used the option to have a trailing /en/ in the URI for the English language in PolyLang, I’ve taken a delve into class-wp-mobile-menu-core.php.
The only hacky solution I could come up was this:
if (preg_match("/\/en\/$/", $_SERVER['REQUEST_URI'])) { //Display the English left menu wp_nav_menu( array( 'menu' => 'mobile-menu-en', 'items_wrap' => '<ul id="mobmenuleft" class="%2$s">%3$s</ul>', 'container_class' => 'menu rounded', 'container' => '', 'fallback_cb' => false, 'depth' => 3, 'walker' => new WP_Mobile_Menu_Walker_Nav_Menu( 'left' ), ) ); } else { //Display the Default left menu wp_nav_menu( array( 'menu' => $current_left_menu, 'items_wrap' => '<ul id="mobmenuleft" class="%2$s">%3$s</ul>', 'container_class' => 'menu rounded', 'container' => '', 'fallback_cb' => false, 'depth' => 3, 'walker' => new WP_Mobile_Menu_Walker_Nav_Menu( 'left' ), ) ); //Check if the Left Menu Bottom Widget has any content /* if ( is_active_sidebar( 'mobmleftbottom' ) ) { */ }I mean, this works, but I was wondering if you have a more elegant solution?
Whilst writing this I cut the amount of code needed (in case non-techy types wanted to use it for their own purposes).
$EnglishMobMenu = 'mobile-menu-en'; if (preg_match("/\/en\/$/", $_SERVER['REQUEST_URI'])) { $ActiveMenu = $EnglishMobMenu; } else { $ActiveMenu = $current_left_menu; } wp_nav_menu( array( 'menu' => $ActiveMenu, 'items_wrap' => '<ul id="mobmenuleft" class="%2$s">%3$s</ul>', 'container_class' => 'menu rounded', 'container' => '', 'fallback_cb' => false, 'depth' => 3, 'walker' => new WP_Mobile_Menu_Walker_Nav_Menu( 'left' ), ) );I could go further and implement it into the settings of your plugin for easier management, but I can’t help but feel there is an easier/more elegant way around it. Any suggestions appreciated 🙂
Thanks!
The topic ‘PolyLang/Menu change based on language’ is closed to new replies.