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 badges3 Answers
Reset to default 8OK - 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
版权声明:本文标题:wp query - WP_Query and polylang issue 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736658736a1946321.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论