Support

Account

Home Forums Add-ons Repeater Field Count Repeater Items

Solved

Count Repeater Items

  • Hi everybody

    I would like to display how many items are generated in the repeater field on my frontend. How I am gonna do this?

    Thanks

  • I’ve found the solution:

    $count = count(get_field("fieldname"));

  • How would you do a count of the field in a flexible content type?
    Looks like $count = count(get_field("fieldname")); is always giving 1

  • Try this:

    <?php echo count( get_field('repeater_field') ); ?>

  • Are talking about a repeater that’s in a flex field?

    <?php echo count( get_sub_field('repeater_field') ); ?>

  • @deminoodle I’m having the same issue and I’ve tried all the suggestions but I still get 1 as result. Have you found a solution for it?

  • Having the same issue. Repeater within a flex field and <?php count( get_sub_field('repeater_field') ); ?> always returns 1

    Anyone get past this?

  • I just did a quick test using this code for a nested repeater and it’s working fine for me outputting the number of rows added in each

    
    // repeater
    if (have_rows('repeater')) {
      while (have_rows('repeater')) {
        the_row();
        // nested repeater
        echo count(get_sub_field('nrepeater')),'<br />';
      }
    }
    

    I can’t say what’s causing issues for anyone.

  • Is there any way to count the items in a repeater but to use that value before the while loop?

    I am using it with foundation block grid and want to set the number of columns to the number of repeater items instead of hardcoding it:

       <?php
        // REPEATER NAME
     	  if( have_rows('grid') ): 
     	 
     	  ?>
     	  <div class="grid-x grid-margin-x small-up-1 medium-up-3 large-up-3"  data-equalizer>
        <?php // REPEATER ROW NAME
        while ( have_rows('grid') ) : the_row();  ?>
        <!-- BLOCK GRID  -->  
    
           <?php 
            $grid_image = get_sub_field('grid_image'); 
            $caption_header = get_sub_field('caption_header'); 
            $caption_content = get_sub_field('caption_content');
             $small_size = 'square-small';
             $medium_size = 'square-medium';         
            $small = $grid_image['sizes'][$small_size];
            $medium = $grid_image['sizes'][$medium_size];
           
              ?>
          
               
              <div class="cell">
               <h5  data-equalizer-watch><?php echo $caption_header; ?></h5> 
        	     <img data-interchange="[<?php echo $small; ?>, small], [<?php echo $medium; ?>, medium], [<?php echo $medium; ?>, large], [<?php echo $medium; ?>, xlarge]" alt="<?php echo $grid_image['alt'] ?>" />
        	       <?php echo $caption_content; ?>      
              </div>   
           <?php     
             endwhile; ?>
              </div>
            <?php endif;   ?> 
  • 
    <?php
      // REPEATER NAME
      if( have_rows('grid') ): 
        
        
        $row_count = count(get_field('grid');
        // OR if this is a sub field
        $row_count = count(get_sub_field('grid');
         
    
  • To others who have troubles with repeater fields in flexible content

    
    <?php //Flexible content
    if( have_rows('builder') ) :
        
        while ( have_rows('builder') ) : the_row();
    
            if( get_row_layout() == 'shortcuts' ): 
                
                //Have to make the count before "have_rows"
                //else it will return false
                $count = count( get_sub_field('buttons') );
    
                //Repeater field
                if( have_rows('buttons') ) :
    
                    while ( have_rows('buttons') ) : the_row();
                    
                    endwhile;
    
                endif;
    
            endif;
    
        endwhile;
    
    endif;
  • @martinkolle — THANK YOU. This was driving me bonkers (always getting 1 on a nested repeater within a flexible content field. This worked.

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

The topic ‘Count Repeater Items’ is closed to new replies.