admin管理员组

文章数量:1125072

In the theme I develop, I have a template showing 5 posts using the standard loop:

            <?php
          $my_query = new WP_Query('showposts=5');
          while ($my_query->have_posts()) : $my_query->the_post(); ?>

and so on....

There is a page (Actualites) using this template. Now I added polylang and provided another page (News) which uses the same template. There will not be an English translation for the blog entries, but still I would like the english speaking readers to be directed to the News page which will be "the same" as Actualites (in french).

I would assume that the query above produces the same outcome regardless which page is using the template, but in fact the French page works fine, but in News I see no posts. Any ideas?

In the theme I develop, I have a template showing 5 posts using the standard loop:

            <?php
          $my_query = new WP_Query('showposts=5');
          while ($my_query->have_posts()) : $my_query->the_post(); ?>

and so on....

There is a page (Actualites) using this template. Now I added polylang and provided another page (News) which uses the same template. There will not be an English translation for the blog entries, but still I would like the english speaking readers to be directed to the News page which will be "the same" as Actualites (in french).

I would assume that the query above produces the same outcome regardless which page is using the template, but in fact the French page works fine, but in News I see no posts. Any ideas?

Share Improve this question asked Nov 19, 2014 at 18:07 fekiohfekioh 2531 gold badge2 silver badges6 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 8

OK - I got it, the old posts already on the database do NOT have an english translation and that's the problem with my loop. When I add a new post, I have the option to provide a translation which then appears in the english page.

looking through the docs I found that I can also show the default when there is no translation by something like :

<?php query_posts(array('post_type' => 'post','lang' => 'fr')); // force querying the French posts ?>
if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    <?php global $post;
    if($post_id = pll_get_post($post->ID, pll_current_language())) { // get translated post (in current language) if exists
        $post = get_post($post_id);
        setup_postdata($post);
    }?>

... do what ever you want in your loop ...

set 'lang' => pll_current_language() in your query for get current lang.

$args = array(
    'posts_per_page' => 5,
    'lang' => pll_current_language(), // Retrieve posts in the current language
);

$my_query = new WP_Query($args);

Isn't that easier?

        $paged = get_query_var('page') ?: 1;
        $args = array(
            's'              => get_search_query(),
            'posts_per_page' => 8,
            'paged'          => (int) $paged,
            'fields'         => 'ids',
            'post_type'      => array('any'),
            'tax_query'      => array(
                                    array(
                                    'taxonomy' => 'language',
                                    'field' => 'slug',
                                    'terms' => pll_current_language(),
                                    )
                                ),

        );
        $query = new WP_Query( $args );

本文标签: wp queryWPQuery and polylang issue