您可以通过使用计数器,然后根据需要更新类来实现这一点。下面的代码将创建一个变量$counter 并将其分配给0 如果存在帖子。默认情况下a\'col-md-4\' 是必须的$class, 如果$counter 小于3 $class 将更新为\'col-md-3\' (使其填充4列而不是3列)。然后输出包含存储类的html。最后$counter 为下一个post项目做好了递增准备。
<?php
if ( have_posts() ) {
$counter = 0;
?>
<div class="container">
<div class="row">
<?php
while ( have_posts() ) {
the_post();
$class = \'col-md-4\';
if( $counter < 3 ) {
$class = \'col-md-3\';
}
?>
<div class="col-12 <?php echo $class; ?>">
<?php // content etc here ?>
</div> <!-- .col-12 -->
<?php
$counter++;
} // end while
?>
</div><!-- .row -->
</div><!-- .container -->
<?php } else { // end if ?>
<p>There are no posts to display</p>
<?php } ?>