ACF / PHP中继器计数

时间:2016-02-02 14:38:47

标签: php wordpress loops logical-operators advanced-custom-fields

我有一个使用高级自定义字段的转发器,当计数达到基于迭代计数的总数(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; ?>

1 个答案:

答案 0 :(得分:0)

认为我的逻辑混淆了一点 - 我没有正确使用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语句..