2014-09-24 23 views
2

我正在使用高级自定义字段,并且我想将每3个div包装在一行中。如果有第四个div或2个额外的那么那些将被包装在他们自己的行中。所以打开并关闭一排。WordPress高级自定义字段中继器,连续每3个div包装

我目前有基本的输出,但我目前所有添加计数器的尝试都失败了。任何帮助,将不胜感激

<?php // wrap every 3 divs in a row 

    if(get_field('triple_column_2')): ?> 

    <?php while(has_sub_field('triple_column_2')): ?> 

      <div class="col-sm-4"> 
       <?php the_sub_field('copy'); ?> 
      </div> 

     <?php endwhile; ?> 

    <?php endif; ?> 

回答

3

您可以使用此作为起点。我没有测试过,所以在我的逻辑中可能会出现轻微的问题,但是这会让你走到最后(如果不是全部的话)。

if (get_field('triple_column_2')): ?> 

    <?php $index = 1; ?> 
    <?php $totalNum = count(get_field('triple_column_2')); ?> 

    <row> 
    <?php while (has_sub_field('triple_column_2')): ?> 


     <div class="col-sm-4"> 
      <?php the_sub_field('copy'); ?> 
     </div> 
     <? if ($index % 3 == 0) : ?> 
      <? if ($index < $totalNum) : ?> 
       // more rows, so close this one and start a new one 
       </row> 
       <row> 
      <? elseif ($index == $totalNum) : ?> 
       // last element so close row but don't start a new one 
       </row> 
      <? endif; ?> 

     <? endif; ?> 

    <?php $index++; ?> 
    <?php endwhile; ?> 

<?php endif; ?> 
+0

谢谢!工作就像一个魅力 – RMH 2014-09-24 17:57:47

+0

刚刚意识到我犯了一个错字:elseif($ index = $ totalNum)应该是elseif($ index == $ totalNum)。将更新答案。 – manishie 2014-09-24 18:38:30

相关问题