admin管理员组文章数量:1287882
I have the following problem developing a WordPress theme and I am here to ask you if this solution could be a good solution or if exist some better way to do.
I have a custom WP theme and into this theme there is a main menu. The website is multilanguage (italian and english) so I have to load the Italian main menu (contains the menu item in Italian language) if the visitor is italian and the main menu have to been loaded in the English version if the user is not italian.
So I have found this solution:
In the WP backend I create 2 different menus: Menu 1 and Menu 2 (the first one having voices in Italian language and the second one in English language)
Then inside my theme (into the header.php file) I declare something like it:
First I detect the user language tacking it from $_SERVER['HTTP_ACCEPT_LANGUAGE']
Then, I use this value to show Menu 1 if the $lang value is it* or *Menu 2 if the *$lang value is en
Something like this:
<?php
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
//echo "Language is $lang"; IT SEEMS TO WORK: MY LANGUAGE IS CORRECTLY RECOGNIZED AS it
if($lang == it){ // If the user is Italian load Italian menu
wp_nav_menu(array(
'menu' => 'Menu 1',
'theme_location' => 'primary',
'depth' => 2,
'container' => 'div',
/*'container_class' => 'navbar navbar-default',*/
'menu_class' => 'nav navbar-nav',
'fallback_cb' => 'wp_bootstrap_navwalker::fallback',
'walker' => new wp_bootstrap_navwalker())
);
}
else{ // If the user is not italian load English menu
wp_nav_menu(array(
'menu' => 'Menu 2',
'theme_location' => 'primary',
'depth' => 2,
'container' => 'div',
/*'container_class' => 'navbar navbar-default',*/
'menu_class' => 'nav navbar-nav',
'fallback_cb' => 'wp_bootstrap_navwalker::fallback',
'walker' => new wp_bootstrap_navwalker())
);
}
?>
I think that in this way have to correctly work (I have to try) but, is it a good solution or could I do better in some other way?
I have the following problem developing a WordPress theme and I am here to ask you if this solution could be a good solution or if exist some better way to do.
I have a custom WP theme and into this theme there is a main menu. The website is multilanguage (italian and english) so I have to load the Italian main menu (contains the menu item in Italian language) if the visitor is italian and the main menu have to been loaded in the English version if the user is not italian.
So I have found this solution:
In the WP backend I create 2 different menus: Menu 1 and Menu 2 (the first one having voices in Italian language and the second one in English language)
Then inside my theme (into the header.php file) I declare something like it:
First I detect the user language tacking it from $_SERVER['HTTP_ACCEPT_LANGUAGE']
Then, I use this value to show Menu 1 if the $lang value is it* or *Menu 2 if the *$lang value is en
Something like this:
<?php
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
//echo "Language is $lang"; IT SEEMS TO WORK: MY LANGUAGE IS CORRECTLY RECOGNIZED AS it
if($lang == it){ // If the user is Italian load Italian menu
wp_nav_menu(array(
'menu' => 'Menu 1',
'theme_location' => 'primary',
'depth' => 2,
'container' => 'div',
/*'container_class' => 'navbar navbar-default',*/
'menu_class' => 'nav navbar-nav',
'fallback_cb' => 'wp_bootstrap_navwalker::fallback',
'walker' => new wp_bootstrap_navwalker())
);
}
else{ // If the user is not italian load English menu
wp_nav_menu(array(
'menu' => 'Menu 2',
'theme_location' => 'primary',
'depth' => 2,
'container' => 'div',
/*'container_class' => 'navbar navbar-default',*/
'menu_class' => 'nav navbar-nav',
'fallback_cb' => 'wp_bootstrap_navwalker::fallback',
'walker' => new wp_bootstrap_navwalker())
);
}
?>
I think that in this way have to correctly work (I have to try) but, is it a good solution or could I do better in some other way?
Share Improve this question edited Mar 3, 2014 at 23:11 AndreaNobili asked Mar 3, 2014 at 22:23 AndreaNobiliAndreaNobili 8786 gold badges21 silver badges36 bronze badges 1- Hm. Is this about switching menus or figuring out which language a user has chosen? – kaiser Commented Mar 3, 2014 at 22:43
3 Answers
Reset to default 1I had the same problem. I used Polylang plugin and 3 languages. So i created 3 menus like this: https://www.diigo/item/image/4o9rz/opx5. after that i checked the language and displayed the correct menu,like this:
<?php
if (get_locale() == 'en_US') {
wp_nav_menu(array(
'menu' => 'Primary Menu En',
'depth' => 2,
'container' => false,
'menu_class' => 'nav navbar-nav',
'walker' => new wp_bootstrap_navwalker()
));
} else if (get_locale() == 'ro_RO') {
wp_nav_menu(array(
'menu' => 'Primary Menu Ro',
'depth' => 2,
'container' => false,
'menu_class' => 'nav navbar-nav',
'walker' => new wp_bootstrap_navwalker()
));
} else if (get_locale() == 'ru_RU') {
wp_nav_menu(array(
'menu' => 'Primary Menu Ru',
'depth' => 2,
'container' => false,
'menu_class' => 'nav navbar-nav',
'walker' => new wp_bootstrap_navwalker()
));
}
?>
Most Websites that uses Multi Lingual WordPress uses WPML plugin. It has a build in Menu Sync feature that will add different version/translation of menu for each language. Its pretty much the standard to go nowadays in terms of WordPress sites having more than one language. Check this link for more info:
https://wpml/documentation/getting-started-guide/translating-menus/
I am not sure if this is the correct solution but as I am also using custom theme and so on I leaned towards creating menus in such manner I can use the advantage of the "suffixes" or "slugs" of the language plugin to determine the current language.
so create suffix variable string
$lang_suffix = "_". pll_current_language('slug');
And then just call for it. Surely you need to create on your own few menus which will need to be in convention "Menu_EN"..."Menu_ES"... and so on. Also little bit of if() checks would be good not to bump into non existing value. But you got the point.
$lang_suffix = "_". pll_current_language('slug');
wp_nav_menu( array(
'menu' => 'Menu'.$lang_suffix,
//'theme_location' => 'primary-menu',
'depth' => 5,
'container' => 'ul',
'container_class' => '',
'menu_class' => 'text-uppercase navbar-nav ml-auto my-2 my-lg-0',
'fallback_cb' => 'wp_bootstrap_navwalker::fallback',
'walker' => new wp_bootstrap_navwalker())
);
?>
本文标签:
版权声明:本文标题:php - How to correctly load a different version of main menu based on the user language in WordPress? Is it a good solution? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741269666a2369042.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论