Hey everyone,
I have a repeater field of 12 images, i am now trying to use the code below to access only the first 4, then later on 4-8, then 9-12,
how can i achieve this by editing the code below?
<?php if( have_rows('repeater_field_name') ): ?>
<ul>
<?php while( have_rows('repeater_field_name') ): the_row(); ?>
<li>sub_field_1 = <?php the_sub_field('sub_field_1'); ?>, sub_field_2 = <?php the_sub_field('sub_field_2'); ?>, etc</li>
<?php
$sub_field_3 = get_sub_field('sub_field_3');
// do something with $sub_field_3
?>
<?php endwhile; ?>
</ul>
<?php endif; ?>
Thanks for any suggestions!
Hi @khadesa
The easiest way would be something like this:
<?php if( have_rows('repeater_field_name') ): ?>
<ul>
<?php $i = 1; ?>
<?php while( have_rows('repeater_field_name') ): the_row(); ?>
<?php if($i <= 4 ){ ?>
<li>sub_field_1 = <?php the_sub_field('sub_field_1'); ?>, sub_field_2 = <?php the_sub_field('sub_field_2'); ?>, etc</li>
<?php
$sub_field_3 = get_sub_field('sub_field_3');
// do something with $sub_field_3
?>
<?php } ?>
$i++;
<?php endwhile; ?>
<?php $i = 1; ?>
<?php while( have_rows('repeater_field_name') ): the_row(); ?>
<?php if($i >= 5 && $i <= 8 ){ ?>
<li>sub_field_1 = <?php the_sub_field('sub_field_1'); ?>, sub_field_2 = <?php the_sub_field('sub_field_2'); ?>, etc</li>
<?php
$sub_field_3 = get_sub_field('sub_field_3');
// do something with $sub_field_3
?>
<?php } ?>
$i++;
<?php endwhile; ?>
<?php $i = 1; ?>
<?php while( have_rows('repeater_field_name') ): the_row(); ?>
<?php if($i >= 9 ){ ?>
<li>sub_field_1 = <?php the_sub_field('sub_field_1'); ?>, sub_field_2 = <?php the_sub_field('sub_field_2'); ?>, etc</li>
<?php
$sub_field_3 = get_sub_field('sub_field_3');
// do something with $sub_field_3
?>
<?php } ?>
$i++;
<?php endwhile; ?>
</ul>
<?php endif; ?>
You can also get the repeater and store it in a variable first like this:
$repeater = get_field('repeater_field_name');
Then you can loop it using foreach.
I hope this helps.