WordPress高级自定义字段转发器,连续包装每3个div

WordPress高级自定义字段转发器,连续包装每3个div

问题描述:

I'm using Advanced Custom Fields, and I would like to wrap every 3 divs in a row. If there is a fourth div or 2 extra then those would get wrapped in their own row. So open and close with a row.

I currently have the basic output but all my current attempts to add the counter have failed. Any help would be appreciated

<?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个div。 如果有第四个div或2个额外的那么那些将被包裹在他们自己的行中。 所以打开和关闭一排。 p>

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

 &lt;?php //连续包裹每3个div 
 
 if(get_field('triple_column_2')):?&gt; \  n 
&lt;?php while(has_sub_field('triple_column_2')):?&gt; 
 
&lt; div class =“col-sm-4”&gt; 
&lt;?php the_sub_field('copy')  ;  ?&gt; 
&lt; / div&gt; 
 
&lt;?php endwhile;  ?&gt; 
 
&lt;?php endif;  ?&gt; 
  code>  pre> 
  div>

You can use this as a starting point. I haven't tested it so there might be slight problems in my logic, but this will get you most of the way there (if not all the way!).

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