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; ?>
我想在 col-6 div 中列印前 4 個結果,在 col-4 中列印其余結果。我正在嘗試匹配陣列的索引
if($results $key < 3) :
<div class="col-sm-6"></div>
else :
<div class="col-sm-4"><div>
endif;
有沒有辦法我可以做到這一點
uj5u.com熱心網友回復:
只需將陣列索引傳遞到回圈中,然后在輸出時根據類名檢查它,例如:
<?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; ?>
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/405237.html
標籤:
上一篇:如何將ACF欄位添加到WooCommerce管理訂單串列上的自定義列
下一篇:多站點上的URL引數處理不正確
