我想将循环分成总循环数的一半。如果总循环数为奇数,我想删除最后一个数组项,以使总循环数为偶数,并将删除的项添加到后半部分。这是演示循环的代码结构-<?php$faqs = new WP_Query( array ( 'post_type' => 'faq'));?><div class="row"> <div class="col-lg-6"> <!-- The first half of the tootal loop count. --> <?php while ($faqs->have_posts()) : $faqs->the_post(); the_content(); endwhile(); ?> </div> <div class="col-lg-6"> <!-- The second half of the tootal loop count. --> <?php while ($faqs->have_posts()) : $faqs->the_post(); the_content(); endwhile(); ?> </div></div>我不知道如何根据我提到的条件控制循环。这就是我无法尝试循环控制的原因。
2 回答
慕少森
TA贡献2019条经验 获得超9个赞
在一个循环中。希望它会起作用。无法测试,所以如果您遇到任何情况,请告诉我。
<?php
$faqs = new WP_Query([
'post_type' => 'faq',
'post_status' => 'publish',
]);
$half = intval($faqs->post_count / 2);
$counter = 0;
?>
<div class="row">
<div class="col-lg-6">
<?php while ($faqs->have_posts()) : $faqs->the_post(); ?>
<?php if ( $counter === $half ) : ?>
</div><div class="col-lg-6">
<?php endif; ?>
<?php the_content(); ?>
<?php ++$counter; endwhile; ?>
</div>
</div>
- 2 回答
- 0 关注
- 147 浏览
添加回答
举报
0/150
提交
取消