Support

Account

Home Forums Add-ons Repeater Field Count total number of repeater rows

Solved

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);
    }
    
  • John,

    Thanks again for your help. That fixed it!

    Daniel

Viewing 9 posts - 1 through 9 (of 9 total)

The topic ‘Count total number of repeater rows’ is closed to new replies.