我按照自定义字段值的降序排列帖子,我想知道是否有方法按降序查找第n个帖子。WordPress的 - 如何获得循环的第n个职位?
例,顺序是:
1st from top: id = 9
2nd from top: id = 5
3rd from top: id = 6
现在,我使用get_template_part()
显示帖子。
我想知道是否有什么东西get_template_part_of_post(3rd-from-top)
。
<div class="onethird">
<?php
$count_posts = wp_count_posts("ott_products", "");
$published_posts_orig = $count_posts->publish;
$published_posts = $published_posts_orig + (3 - ($published_posts_orig % 3));
$i = 0;
if (have_posts()) : while($query->have_posts()) :
echo $i . " " . $published_posts;
$i = $i + 3;
$query->the_post();
get_template_part('content', 'category');
if ($i % 3 === 2) :
if (($i - 2 == $published_posts)) :
$i = 3;
endif; endif;
if ($i % 3 === 1) :
if (($i - 1 == $published_posts)) :
echo "</div><div class='onethird last'>";
$i = 2;
endif; endif;
if ($i % 3 === 0) :
if (($i == $published_posts)) :
echo "</div><div class='onethird'>";
$i = 1;
endif; endif;
endwhile;
else :
get_template_part('no-results', 'archive');
endif;
?>
</div>
这就是我目前使用的。这将帖子分成三列。
变量i
将从上到下的三列变为从左到右。
以前,我有显示类似的帖子:
(Total 9 posts)
1 4 7
2 5 8
3 6 9
有了它,我得到的i
到:现在
(Total n posts)
1 2 3
4 5 6
...
,问题是,我不能让i
日发布显示。帖子仍然进来第一顺序。
感谢您的答案,我编辑了上面的代码,以解释为什么这不起作用。 – NamanyayG 2013-02-08 16:05:13