ACF / PHP中继器计数


ACF / PHP Repeater count

我有一个使用高级自定义字段的中继器,当计数根据迭代计数达到总数(3)时,我想输出一个div,然后重置计数器。

这是代码,目前它没有按应有的方式输出,我对数学的了解不足以让它工作。

感谢您的帮助:-)

      <?php if(have_rows('flexible_row')) : ?>
  <div class="row">
    <?php if(empty($count)){ $count=1;} while(have_rows('flexible_row')) : the_row(); ?>
        <?php if(get_sub_field('column_width') == "One column") { $count = $count+1; ?>
          <div class="col-sm-4">
            <?php the_sub_field('title');?>
            <?php the_sub_field('content');?>
            Count is <?php echo $count; ?>
            <hr/>
          </div>
        <?php } if(get_sub_field('column_width') == "Two columns") { $count = $count+2; ?>
          <div class="col-sm-8">
            <?php the_sub_field('title');?>
            <?php the_sub_field('content');?>
            Count is <?php echo $counter; ?>
            <hr/>
          </div>
        <?php } else { $count = $count+3; ?>
          <div class="col-sm-12">
            <?php the_sub_field('title');?>
            <?php the_sub_field('content');?>
            Count is <?php echo $counter; ?>
            <hr/>
          </div>
      <?php } if ($count == 3) {  ?></div><div class="row"><?php $count = 0; } ?>
    <?php endwhile; ?>
  </div>
<?php endif; ?>

认为我的逻辑有点混乱——我没有正确使用else子句。这是最终的工作代码,以防将来有人偶然发现它:

      <?php if(have_rows('flexible_row')) : ?>
  <div class="row">
    <?php if(empty($count)){ $count=0;} while(have_rows('flexible_row')) : the_row(); ?>
        <?php if(get_sub_field('column_width') == "One Column") { $count = $count+1; ?>
          <div class="col-sm-4">
            <?php the_sub_field('title');?>
            <?php the_sub_field('content');?>
          </div>
        <?php } elseif(get_sub_field('column_width') == "Two Columns") { $count = $count+2; ?>
          <div class="col-sm-8">
            <?php the_sub_field('title');?>
            <?php the_sub_field('content');?>
          </div>
        <?php } else { $count = $count+3; ?>
          <div class="col-sm-12">
            <?php the_sub_field('title');?>
            <?php the_sub_field('content');?>
          </div>
      <?php } if ($count == 3) {  ?></div><div class="row"><?php $count = 0; } ?>
    <?php endwhile; ?>
  </div>

当然,您也可以使用switch语句。