Hi everyone!
My apologies if this has already been addressed elsewhere, however I can’t quite seem to find a solution even after looking at other people’s posts – that said I’m more of a learner in this area.
I’m looking to count the number of rows in a repeater field nested within a flexible content field. I want to append the count value to the class of the container that will wrap my repeater field rows.
I’ve successfully been able to call and echo the count if I use <?php echo count(get_sub_field('nested_repeater')); ?>
after the loop that displays each row. That said, as soon as I try to do this within the class of the wrapper (before the loop), it returns a value of 1, no matter how many rows there are.
Anybody have any thoughts on how to count the number of rows outside/before the loop?
Thanks in advance; let me know if I can provide more info!
That depends on what “before the loop” means
// you cannot get the count of repeater rows here
while(have_rows('flex_field')) {
the_row();
// here you can get the count
$rows = count(get_sub_field('repeater');
}
Hi John, thanks for your reply!
By before the loop, I mean before while(have_rows())
. This is my current setup; looking to insert my count as in double brackets:
<?php if( get_row_layout() == 'flex_field' ):
if( have_rows('repeater') ): ?>
<div class="repeater-wrapper [[ECHO REPEATER ROW COUNT HERE]]">
<?php while ( have_rows('repeater') ) : the_row(); ?>
<div class="repeater-row">
<p><?php the_sub_field('repeater_row_content'); ?></p>
</div>
<?php endwhile; ?>
</div>
<?php endif;
endif; ?>
Apologies if my syntax is a bit messy. Any thoughts on how your solution might work in this instance?
<?php if( get_row_layout() == 'flex_field' ):
$row_count = count(get_sub_field('repeater');
if( have_rows('repeater') ): ?>
<div class="repeater-wrapper <?php echo 'rows-.$row_count'; ?>">
<?php while ( have_rows('repeater') ) : the_row(); ?>
<div class="repeater-row">
<p><?php the_sub_field('repeater_row_content'); ?></p>
</div>
<?php endwhile; ?>
</div>
<?php endif;
endif; ?>