WPML by default has many multilingual switching options, but one thing has always been missing for those who just need a bilingual site and want to switch between two languages without dropdowns or other such default widgets.
This code does a few things you probably only want the last part. You will need to know the name of the registered menu in this case it’s header-menu
Add the code to you themes functions.php
If you are using the Genesis framework the two default menu names are primary and secondary
// Add the new menu
function register_my_menus() {
register_nav_menus(
array(
'header-menu' => __( 'Header Menu' ),
'footer-menu' => __( 'Footer Menu' )
)
);
}
add_action( 'init', 'register_my_menus' );
//language switcher remove css
define('ICL_DONT_LOAD_NAVIGATION_CSS', true);
define('ICL_DONT_LOAD_LANGUAGE_SELECTOR_CSS', true);
/* WPML Main Menu */
function new_nav_menu_itemsmain($items,$args) {
if (function_exists('icl_get_languages') && $args->theme_location == 'header-menu') {
$languages = icl_get_languages('skip_missing=0');
if(1 < count($languages)){
foreach($languages as $l){
if( ! $l['active'] ) {
$items = $items.'<li class="menu-item-'.$l['language_code'].'"><a href="'. $l['url'].'">'.$l['native_name'].'</a></li>';
}
}
}
}
return $items;
}
add_filter( 'wp_nav_menu_items', 'new_nav_menu_itemsmain',10,2 );
The language switcher will be the last item on the menu and will now toggle between the two languages.
With some css you can move it over to the far right away from other menu items.


Leave a Reply