您不需要对它们进行单独的查询。查询所有12篇文章,当您在第一节中输出它们时,仅在以下情况下输出它们$wp_query->current_post < 6
, 然后rewind_posts()
然后再次运行循环,仅在以下情况下在第二部分中输出它们$wp_query->current_post > 5
. 如果使用自定义WP_Query
, 交换$wp_query
使用自定义查询对象的名称。使用检查当前输出的帖子的方法,您甚至可以在一个循环中完成。
EDIT - 实例请注意current_post
从零开始,所以在第六根柱子上等于5:
$the_query = new WP_Query( array(
\'posts_per_page\' => 15
) );
while ( $the_query->have_posts() ) :
$the_query->the_post();
if( $the_query->current_post < 6 ) :
// This is one of the first 6 posts
// Output the thumbnail, title, etc..
endif;
if( $the_query->current_post == 5 ) :
// The sixth post has been output,
// output the header/opening container
// for all the other posts
endif;
if( $the_query->current_post > 5 ) :
// This is post 7 and greater
// Output just the title, etc..
endif;
endwhile;
Example 2 - 使用
rewind_posts
并运行相同的循环两次:
$the_query = new WP_Query( array(
\'posts_per_page\' => 15
) );
while ( $the_query->have_posts() ) :
$the_query->the_post();
if( $the_query->current_post < 6 ) :
// This is one of the first 6 posts
// Output the thumbnail, title, etc..
endif;
endwhile;
// rewind the loop to run it again
$the_query->rewind_posts();
while ( $the_query->have_posts() ) :
$the_query->the_post();
if( $the_query->current_post > 5 ) :
// This is post 7 and greater
// Output just the title, etc..
endif;
endwhile;