无论我是在法语版还是英语版的页面上使用它,wp_query() 都会返回所有语言的自定义帖子类型,而不仅仅是当前的。Get_posts() 也做同样的事情。
当我访问我的法语页面时,我希望他们只返回当前语言的 CPT。如何做到这一点?
使用get_posts() 时,将suppress_filters设置为false:
$myPosts = get_posts(array(
'suppress_filters' => false
));
Run Code Online (Sandbox Code Playgroud)
http://codex.wordpress.org/Function_Reference/get_posts#Parameters
这是我发现使用 WPML 获取特定语言的帖子的最佳方法...
就我而言,我需要按特定语言的标题查找帖子并返回帖子的 ID:
$lang='en';
$title='The title you are searching!';
function getWpIdByTitle($title, $lang){
global $sitepress;
// WPML Super power language switcher...
$sitepress->switch_lang( $lang );
$args = array(
'title' => $title,
'post_type' => 'your-post-type', // Default: post
'post_status' => 'publish',
'suppress_filters' => false,
'numberposts' => 1
);
$wp_query = new WP_Query( $args );
return $wp_query->post->ID;
}
Run Code Online (Sandbox Code Playgroud)
您可以使用 $wp_query->post 作为获取的结果,并对标题、内容等进行回显。
这样你就不需要使用
do_action( 'wpml_set_element_language_details', $set_language_args );
连接您的语言帖子,既不是
icl_object_id(1,'post',false,ICL_LANGUAGE_CODE);
获取特定语言的帖子 ID。
| 归档时间: |
|
| 查看次数: |
10560 次 |
| 最近记录: |