【发布时间】:2014-12-12 15:33:33
【问题描述】:
我有一个正常的循环,根据给定的 $args 输出帖子。 在三个帖子之后,我想插入一个来自精选类别的帖子。我试过以不同的组合开始一个新的 WP_Query,简单的 query_posts。似乎没有任何效果。任何想法为什么?
$args = array(
'post_type' => 'post',
'posts_per_page' => $count,
'paged' => $paged,
'page' => $paged,
'cat' => $cat,
'ignore_sticky_posts' => 1
);
// create a new instance of WP_Query
$my_query = new WP_Query($args);
<ul>
<?php
$i = 0;
if ($my_query->have_posts()) : while ($my_query->have_posts()) : $my_query->the_post();
if($i == 3): ?>
<li> //insert here one post from featured category
</li>
<?php endif; ?>
<li>
// the normal query stuff is here
</li>
<?php $i++ //post counter
endwhile; //end loop while
endif; //end loop
?>
</ul>
【问题讨论】:
-
所以任何其他方式在循环中获取帖子,期望使用 get_post() 可以工作但需要确切的 id ?
-
当您执行 the_post() 时,您应该能够访问和设置来自
$post变量的任何内容。因此,首先运行您的功能查询并将该特色项目的内容设置在其自己的变量中,例如$featured = $post- 然后在您的条件$i == 3中您可以说$feature->post_title、get_permalink($feature->ID)等。您不必运行另一个循环内的循环。 -
Nested_Loops > codex.wordpress.org/The_Loop#Nested_Loops