Home › Forums › Add-ons › Repeater Field › Count total number of repeater rows
Tried multiple solutions. Still stuck on this.
I need to display different classes, based on the number of rows in a repeater field.
...
<?php elseif ( get_row_layout() == 'grid' ) : ?>
<?php if ( have_rows( 'grid_block' ) ) : ?>
<?php while ( have_rows( 'grid_block' ) ) : the_row(); ?>
// if total number of rows > 1 do this for all rows...
// elseif total number of rows > 2 do this for all rows...
// elseif total number of rows > 3 do this for all rows...
<?php endwhile; ?>
<?php endif; ?>
<?php endif; ?>
...
Thanks for any kind of help!
This will give you the number of rows
$count = count(get_field('grid_block'));
Thanks. I found that snippet before. It returns 0 in my case.
Not sure what I’m doing wrong:
...
<?php elseif ( get_row_layout() == 'grid' ) : ?>
<?php $count = count(get_field('grid_block')); ?>
<?php echo $count; ?> // No matter where I place this, it returns 0
<?php if ( have_rows( 'grid_block' ) ) : ?>
<?php while ( have_rows( 'grid_block' ) ) : the_row(); ?>
// if total number of rows > 1 do this for all rows...
// elseif total number of rows > 2 do this for all rows...
// elseif total number of rows > 3 do this for all rows...
<?php endwhile; ?>
<?php endif; ?>
<?php endif; ?>
...
Any thoughts on this?
Not sure how to solve this unfortunately.
Grid block is a sub field of another field, try 1 get_sub_field('grid_block')
So i’ve been using the syntax to get the count of a repeater’s rows from this thread successfully until I recently upgraded my PHP from 5 to 7. After upgrading to 7, I get the following warning:
Warning: count(): Parameter must be an array or an object that implements Countable in /nfs/c06/h08/mnt/153609/domains/mydomain.com/html/wp-content/themes/mytheme/template-name.php on line 135
The code in the template is as follows:
<?php $count = count(get_field("locations")); ?>
I then use the count in a conditional if/then statement.
It all still works, but throws the warning if php error reporting is on and I like to resolve it, but not sure how.
Actually, it shows even when error reporting is off. So i’ll have to fix it somehow
$count = 0;
$locations = get_field('locations');
if (is_array($locations)) {
$count = count($locations);
}
The topic ‘Count total number of repeater rows’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.