0

我有一個使用高級自定義字段的中繼器,當計數達到基於迭代計數的總數(3)時,我想輸出一個div,然後重置計數器。ACF/PHP直放站數

下面是代碼,目前它不是輸出,因爲它應該和我不知道數學得到它的工作。

感謝您的幫助:-)

 <?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; ?> 
+0

問題1:我用的是'回聲$ counter'而不是' echo $ count',這已經解決了第一個問題! – endymion1818

回答

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語句太..解決