Simple WPML language switch

公司客户老喜欢吧 语言选择器 做成这样~ 麻烦~

function icl_post_languages(){
  $languages = icl_get_languages('skip_missing=0&orderby=code');
  if(1 < count($languages)){
 		$langs[] = '<div class="languages">';
    foreach($languages as $l){
        if($l['language_code'] == 'zh-hans') $l['language_code'] = "中文";
      	$active = $l['active'] == 1?"class='active_language'":""; 
        $langs[] = '<a '.$active.' href="'.$l['url'].'">'.$l['language_code'].'</a>';
    }
    $langs[] = '</div>';
    echo join("\n", $langs);
  }
}

文章( Post)和 分类目录(category)页面不显示多语言选择链接

 if (icl_post_languages()):
	$post_types=get_post_types(array(  'public'   => true,  '_builtin' => false)); 
	if(is_page() || is_singular($post_types)) echo '<div class="icl_post_languages">' . icl_post_languages('skip_missing=1') . '</div>';
 endif;

加到主导航的最后,这个功能其实默认已经有了,只是常常不是我们要的,我们要的是这样:

wpml

add_filter('wp_nav_menu_items', 'itc_wp_nav_menu_items_filter', 10, 2);

function itc_wp_nav_menu_items_filter($items, $args){
	if($args->theme_location == 'primary'){
		$items .= '<li class="menu-item menu-item-language menu-item-language-current">';
		$items .= itc_icl_post_languages();
		$items .= '</li>';
		return $items;
	}else{
		return '';
	}
}
function itc_icl_post_languages(){
	$languages = icl_get_languages('skip_missing=0&orderby=code');
	if(1 < count($languages)){
		$langs = array();
		foreach($languages as $l){
			if($l['language_code'] == 'zh-hans') $l['language_code'] = "CN";
			$active = $l['active'] == 1?"class='active_language'":"";
			$langs[] = '<a '.$active.' href="'.$l['url'].'">'.$l['language_code'].'</a>';
		} 
		return  '<span class="languages">' . join("/", $langs) .'</span>';
	}
}