I have a repeater field inside a flexible content block.
I need to count the number of repeater rows so I can give class to the wrapper, for columns.
Setup like so, you can see the $num_cols
variable I am trying to fill in with a count, obviously does not work 🙂
if( have_rows('columns') ) :
$num_cols = count( get_sub_field('columns') ); ?>
<div class="grid-col-<?php echo $num_cols; ?>">
<?php
while ( have_rows('columns') ) : the_row(); ?>
<div class="column">
<p class="column-title"><?php the_sub_field('title'); ?></p>
</div>
<?php endwhile; ?>
</div>
<?php endif; ?>
I guess this works:
$num_cols = count( get_field('columns') );
That does not work no… Had tried that before as well
if the repeater is a sub field of a flex field then you need to use get_sub_field()
$num_rows = count(get_sub_field('columns'));
Yeah I had tried that as well, that is the second line in my code block above 🙂
Here is how it is setup, the repeater is in a flexible content set,

Do you have a loop for the flex field?
if (have_rows('flex_field_name')) {
while (have_rows('flex_field_name')) {
if (get_row_layout() == 'column_blocks') {
$num_rows = count(get_sub_field('columns'));
// etc...
}
}
}