WPML:Get posts also available in other languages

文章页面里,在文章的底部显示该篇文章对应的翻译的语种(国旗)和标题。

暂时没有发现有现成的函数或 API 接口。但是从WPML 的表里面不难看出他们的关系,通过一句SQL即可找出当前文章对应文章的ID, 如下:

function _post_foot_languages(){
  global $post, $wpdb, $sitepress;
  $active_languages = $sitepress->get_active_languages();
  unset($active_languages[ICL_LANGUAGE_CODE]);
  //print_r(array_keys($active_languages));
  $source_language_code = "";
  if(ICL_LANGUAGE_CODE != $sitepress->get_default_language())
	$source_language_code = " and source_language_code = '".$sitepress->get_default_language()."'";
  $query ="
SELECT  *
FROM  {$wpdb->prefix}icl_translations
WHERE  trid = ( 
SELECT  trid
FROM  {$wpdb->prefix}icl_translations
WHERE  element_id = {$post->ID}{$source_language_code})
AND language_code in ('".join('\',\'',array_keys($active_languages))."')";
//AND language_code != '".ICL_LANGUAGE_CODE."'";

  //echo	$query;
  $get_translate = $wpdb->get_results($query);
  //print_r($get_translate);
  $template_url = get_bloginfo('stylesheet_directory');
  if(0 < count($get_translate)){
    $html[] = '<h3>';
    $html[] = __('This post is also available in other languages', 'twentyeleven' );
    $html[] = '</h3>';
	$html[] = '<ul class="this_translates">';
    foreach($get_translate as $l){
       if (get_post_status($l->element_id) === "publish"){
		$html[] = '<li><img src="'. plugins_url( 'sitepress-multilingual-cms/res/flags/'.$l->language_code.'.png') .'" alt="'.get_the_title($l->element_id).'"> <a href="'.get_permalink($l->element_id).'">'.get_the_title($l->element_id) .'</a></li>';
         }
    }
	$html[] = '</ul>';
    return join("\n", $html);
  }
}

function _add_post_wpml_translate_title($content) {
	if (is_single()) {
		$content .= _post_foot_languages();
	}
	return $content;
}

add_filter ('the_content', '_add_post_wpml_translate_title', 0);