Lets say you are using Advanced Custom Fields, and I would like to wrap every 3 divs (3 elements, 4 things, any number of consecutive stuff) in a row. If there is a fourth div or 2 extra then those would get wrapped in their own row. So no matter how many items you will open and close with a div etc.
You’ll want to use the following:
<?php //going to wrap every 3 in this example
if ( get_field( 'your_repeater_name' ) ): ?>
<?php $index = 1; ?>
<?php $totalNum = count( get_field('your_repeater_name') ); ?>
<row>
<?php while ( has_sub_field( 'your_repeater_name' ) ): ?>
<div class="col-sm-4">
<?php the_sub_field( 'your_sub_field' ); ?>
</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; ?>
Hope this helps!