How to count the total number of rows in a ACF repeater output
It's work for me, count must be placed before if(have_rows('repeater_field') :
Ternary operator to avoid warning errors if repeater is empty
If you place the count after "if(have_rows('repeater_field')) :", count returns FALSE
$repeater_field = get_sub_field('repeater_field');
// OR if repeater isn't a sub_field
// $repeater_field = get_field('repeater_field');
// ternary operator to avoid warning errors if no result
$count = $repeater_field ? count($repeater_field) : FALSE;
if(have_rows('repeater_field')) : // OR if($count) :
echo 'Number of posts:' . $count . '<br>';
while(have_rows('repeater_field')) : the_row();
echo get_sub_field('field_name') . '<br>';
endwhile;
endif;
You can get the row count like this:
$count = get_post_meta(get_the_ID(), 'testimonials', true);
Obviously this uses get_the_ID()
to retrieve the current post ID - you may need to amend this.
ACF stores the repeater count value against the repeater field name as the meta_key in the postmeta table.
ACF uses the count to retrieve the correct repeater subfield values, which are stored as values against meta_keys with the format $repeaterFieldname . '_' . $index . '_' . $subfieldName
.
Hope this helps...
OK, I finally found the answer to this.
The way to count total rows in an ACF repeater is:
$numrows = count( get_sub_field( 'field_name' ) );