4

无论我是在法语版还是英语版的页面中使用它,wp_query() 都会以所有语言返回我的自定义帖子类型,而不仅仅是当前语言。Get_posts() 也做同样的事情。

当我以法语访问我的页面时,我希望他们仅以当前语言返回 CPT。如何做到这一点?

4

2 回答 2

6

使用get_posts()时,将suppress_filters设置为false

$myPosts = get_posts(array(
    'suppress_filters' => false
));

http://codex.wordpress.org/Function_Reference/get_posts#Parameters

于 2015-03-07T09:13:50.263 回答
0

That's the best way I found to fetch posts on a specific language using WPML...

In my case I need to find a post by it's title on a specific language and return the ID of the post:

$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;
    }

You may use the $wp_query->post as the result of the fetch and do the echo's of title, content, etc.

This way you don't need to use the

do_action( 'wpml_set_element_language_details', $set_language_args );

to connect your language posts, neither the

icl_object_id(1,'post',false,ICL_LANGUAGE_CODE);

to get the ID of a post on a specific languange.

于 2018-10-21T21:51:56.657 回答