我有一个推荐轮播。轮播每次循环通过两个项目。现在我想在foreach循环中每次获得两个项目。我怎么得到它?
代码:
<?php foreach ($kiyra_section_meta['testimonials-group'] as $single_testimonial): ?>
<div>
<div class="row">
<div class="col-md-6">
<div class="single-review">
<div class="media">
<div class="media-body">
<h3 class="mt-0"><?php echo esc_html($single_testimonial['client-name']); ?></h3>
<h5><?php echo esc_html($single_testimonial['client-position']); ?></h5>
<i class="fas fa-quote-right fa-5x"></i>
</div>
</div>
<?php echo esc_html($single_testimonial['client-testimonial']); ?>
</div><!--/.single-review-->
</div>
</div>
</div>
<?php endforeach; ?>
答案 0 :(得分:3)
您可以使用array_chunk将数组拆分为第2组项目,然后再次使用foreach
foreach (array_chunk($input_array, 2) as $group) {
// Start Group
foreach ($group as $item) {
// Item
}
// End group
}
使用HTML更新
<div class="owl-carousel">
<?php foreach (array_chunk($input_array, 2) as $group) : ?>
<div class="owl-item">
<?php foreach ($group as $item) : ?>
<div class="item">
<!-- Code of item -->
</div>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
</div>