这可以通过访问WP_Query
班properties, 在循环中可用,如$current_post
(当前职位索引),$post_count
(显示的帖子数量),$found_posts
(与查询匹配的帖子总数)。
通过使用$current_post
, $post_count
和PHPmodulo, 如下图所示:
<?php if ( $custom_query->have_posts() ) : while ( $custom_query->have_posts() ) : $custom_query->the_post();
$i = $custom_query->current_post;
$c = $custom_query->post_count;
if ( $i % 4 == 0 ) {
// happening for $i being 0, 4, 8 and so on
echo \'<div class="row">\';
} ?>
// single post markup
<?php if ( $i % 4 == 3 || $i == ( $c - 1 ) ) {
// happening for $i being 3, 7, 11 and so on
// OR condition in case the last one is not a multiple of 4
echo \'</div>\';
}
endwhile; endif; ?>