• Resolved fiddles

    (@fiddles)


    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!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Rui Guerreiro

    (@takanakui)

    Hi @fiddles,

    Good job on the implementation.

    It seems Polylang uses the theme location instead the menu name. Check the link below.
    https://polylang.wordpress.com/documentation/setting-up-a-wordpress-multilingual-site-with-polylang/navigations-menus/

    In this case check what is the theme location you want to use( Appearance-> Menus-> Manage Locations)

    And in the class-wp-mobile-menu-core.php try this code instead, and replace primary by the correct theme location(use the slug,e.g: if it says Main Navigation the slug will be main-navigation)

    
    $ActiveMenu = 'primary'
    wp_nav_menu( array(
                        'theme_location'            => $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' ),
                    ) );	

    Let me know if it works.

    Thread Starter fiddles

    (@fiddles)

    Ah, thanks for pointing me to the polylang page, I had assumed it was working on a different principle – I’ll try that code out later.

    Cheers

    • This reply was modified 8 years, 8 months ago by fiddles. Reason: More than likely resolved
    Plugin Author Rui Guerreiro

    (@takanakui)

    no problem, let me know if it worked or not so we can find an alternative solution.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘PolyLang/Menu change based on language’ is closed to new replies.