foreach($results as $result) :
$featured_image=wp_get_attachment_url(get_post_thumbnail_id($result->ID));?>
<div class="col-sm-6">
<a href="javascript:;">
<figure>
<img src="<?php echo $featured_image ?>">
<figcaption>
<h2><?php echo get_the_title($result->ID) ?></h2>
<p><?php echo get_the_content($result->ID) ?></p>
</figcaption>
</figure>
</a>
</div>
<?php endforeach; ?>
我想将前4个结果打印在第6列的div中其余在第4栏。我正在尝试匹配arrray 的索引
if($results $key < 3) :
<div class="col-sm-6"></div>
else :
<div class="col-sm-4"><div>
endif;
有办法做这个吗
只需将数组索引传递到循环中,然后在输出时根据类名进行检查,例如:
<?php
foreach($results as $i => $result) :
$featured_image=wp_get_attachment_url(get_post_thumbnail_id($result->ID));
?>
<div class="col-sm-<?= ($i < 4) ? 6 : 4 ?>">
<a href="javascript:;">
<figure>
<img src="<?php echo $featured_image ?>">
<figcaption>
<h2><?php echo get_the_title($result->ID) ?></h2>
<p><?php echo get_the_content($result->ID) ?></p>
</figcaption>
</figure>
</a>
</div>
<?php endforeach; ?>