Add WPML language switch without current language


This is a convenient way to add a link in the main menu to switch languages without including the active language. You’d figure this would be included in WPML but this doesn’t seem to be very high on their priority list.

This code works only if you’re using 2 languages but its easy to customize if needed.

Go to Appearance->Editor and open Theme Functions (functions.php)  or edit functions.php in your theme’s folder using your favorite text editor.

Add this code to the end of your functions.php file:

add_filter('wp_nav_menu_items','add_wpml_language_switch', 10, 2);
function add_wpml_language_switch($items, $args) 
{
    if( $args->theme_location == 'primary' ) {

        $languages = icl_get_languages('skip_missing=1');
        if(1 < count($languages)){
            foreach($languages as $l){
                if(!$l['active'])
                return $items . '<li class="menu-item menu-item-language"><a href="'.$l['url'].'">'.$l['translated_name'].'</a></li>';
            }
        }       
    }
}

Note: For advanced users, create a child theme if you want to use this snippet without modifying your theme’s core files. This way you will be able to update your theme without losing your custom code.

[ad name=”HTML”]