我想更详细一点(或你的起始代码),看看你把它放在哪里,或者你想如何使用它,但从我看来,你需要在迭代中添加一个计数器,然后使用模运算符:
示例查询如下所示:
$winnners_query = new WP_Query(array(
\'post_type\' => \'post\',
\'posts_per_page\' => \'10\',
));
if ($winnners_query->have_posts()) {
$i=1;
$section=1; //section counter
$count = $winnners_query->found_posts; //total post count
echo \'<section class="side" data-role="side" id="section-section\'.$section.\'">\'; //adds first section (you need to add css to style)
while ($winnners_query->have_posts()) {
$winnners_query->the_post();
echo \'<div class="content">\'.get_the_title().\'</div>\'; //adds content you need to add css styles
if ( $count == $i) {
//we\'re on the last post of the query. Close section
echo \'</section>\';
} else {
if ($i % 2) { //if iteration is even increase section count, close section and open a new one
$section++;
echo \'</section><section class="side" data-role="side" id="section-section\'.$section.\'">\';
}
$i++;
}
}
wp_reset_postdata();
}