为了账号安全,请及时绑定邮箱和手机立即绑定

如果循环计数是偶数,则将循环分成两半

如果循环计数是偶数,则将循环分成两半

PHP
慕容708150 2021-06-10 10:41:54
我想将循环分成总循环数的一半。如果总循环数为奇数,我想删除最后一个数组项,以使总循环数为偶数,并将删除的项添加到后半部分。这是演示循环的代码结构-<?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>


查看完整回答
反对 回复 2021-06-19
  • 2 回答
  • 0 关注
  • 147 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信