Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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:

  1. First I detect the user language tacking it from $_SERVER['HTTP_ACCEPT_LANGUAGE']

  2. 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
    
Hm. Is this about switching menus or figuring out which language a user has chosen? –  kaiser Mar 3 at 22:43

1 Answer 1

I had the same problem. I used Polylang plugin and 3 languages. So i created 3 menus like this: https://www.diigo.com/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()
                ));
            }
         ?>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.