Robert
Robert

Reputation: 774

How to count the total number of rows in a ACF repeater output

Question: How do you simply count the rows in the output of an ACF repeater field?

Goal: to make the output look different with a css class when there's only one row, vs. more than one row.

My code:

if( have_rows('testimonials')) {
    $counter = 0;
    $numtestimonials = '';

    //loop thru the rows
    while ( have_rows('testimonials') ){
        the_row();
        $counter++;
            if ($counter < 2) {                         
                $numtestimonials = 'onlyone';               
            }
        echo '<div class="testimonial ' . $numtestimonials . '">';
           // bunch of output here
        echo '</div>';                          
    }
}

Obviously, The way I do it here will not work as the count is < 2 the first time thru the row, so it returns true even if more rows are counted after.

Thank you!

Upvotes: 18

Views: 46721

Answers (8)

Otis Feru
Otis Feru

Reputation: 151

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;

Upvotes: 8

Majid Shah
Majid Shah

Reputation: 1

ACF Repeater Field uses array to store the rows The simplest way to get the numuber of field rows is

$count = sizeof(get_sub_field('field_name'));

Upvotes: -1

Jake
Jake

Reputation: 405

Advanced Custom Fields has a built in function to count the rows added in version 5.3.4.

Official Documenation: ACF | get_row_index()

You can use get_row_index(); inside the loop.

<?php if( have_rows('slides') ): ?>
    <?php while( have_rows('slides') ): the_row(); ?>
        <div class="accordion" id="accordion-<?php echo get_row_index(); ?>">
            <h3><?php the_sub_field('title'); ?></h3>
            <?php the_sub_field('text'); ?>
        </div>
    <?php endwhile; ?>
<?php endif; ?>

The index returned from this function begins at 1. This means a Repeater field with 3 rows of data will produce indexes of 1, 2 and 3.

Upvotes: 1

KennethRules
KennethRules

Reputation: 11

You may wanted to try this..

<?php 
$row = get_field('repeater', $post->ID);
if($row < 1) {
 $rows = 0;
} else {
 $rows = count($row);
} ?>
<p>Number of Row is (<?php echo $rows ; ?>)</p>

Upvotes: 1

Anuruddh Pratap
Anuruddh Pratap

Reputation: 22

You can try this code its working fine :

<?php if( have_rows('repeater_name') ):
$my_fields = get_field_object('repeater_name');
$count = (count($my_fields));
echo $count;
endif;?>

Upvotes: -1

Dhruv Saxena
Dhruv Saxena

Reputation: 1346

Looks like you would want to reset the value in $numtestimonials.

So, effectively, the code with a fix would be:

$output = "";
$numtestimonials = "";

while ( have_rows('testimonials') ){
    the_row();
    $counter++;
    $output .= "<span>" .$some_data. "</span>"; // bunch of output;
}

if($counter < 2){
    $numtestimonials = "onlyone";
}
$output = "<div class='testimonail ".$numtestimonials." '>"
              .$output
         ."</div>";
echo $output;

Upvotes: 0

Robert
Robert

Reputation: 774

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' ) );

Upvotes: 44

csknk
csknk

Reputation: 2039

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...

Upvotes: 3

Related Questions