ACF Loop Quantity

Hi, i have a repeater ACF Field for a CPT. The Loop is working, but at the Moment it shows ALL Data/Lines from ACF Repeater. For example i only wont to show the first three, instead of all. Is there a easy solution for that?

1 Like

Well, you can limit the number of rows that can de added in ACF settings, but that doesn’t affect already added fields.
There must be a code snippet to do that for you, but I didn’t find it.
With CSS, you can set display to none from nth child of the repeater. You just have to target it right. But that does not seem the right thing to do.

:nth-of-type(1n+2){ display: none; }

You can create a new CPT instead of ACF repeater too.
Or maybe do it with a code block.

Would love to bump this to a Feature Request - to have a section within Query loop to set the number of items shown and starting from what number item. @timmse

2 Likes

Hey Ace,
Feel free to submit your request to the idea board. That’s the official place for requests like this:

1 Like

@timmse - wish this could be achieved somehow, is there any code snippet that could limit the no. of posts.

If this is the only place you output posts you can set the number of post in the admin: Settings → Reading → Blog pages show at most

Good morning

in case somebody wants to use a code element to show an acf repeater with a defined # of loops.

Below is my code that I use for my own pages with acf repeaters. It loops over a repeater and shows 3 lines from the sub-repeater (=child) of the repeater (=father). You can easily modify it if you only want to loop over the repeater (=father) in case you do not have a sub-repeater (=child).

Put this into a code element and adjust the acf repeater field names (e.g. acf_repeater, acf_titel, etc.) and enter the required number of loops in the line “for ($i = 1; $i <= 3; $i++)”.

Cheers

Patric

<?php 
   /* first we check, if the acf repeater has any content */
   if( have_rows('acf_repeater') ):
             
             /* if it has content, we loop over all content */
             while ( have_rows('acf_repeater') ) : the_row();
             
             /* as an example, we get a text field from the repeater and print it */
             $repeater_titel = get_sub_field('acf_titel');
             
               if ($repeater_titel <> "") {
               echo '<span class="titel">'.$repeater_titel.'</span>';
               }
     
             /* now we check if the sub_repeater has any content */
             if( have_rows('acf_sub_repeater') ):
			
			 /* if it has content, we loop over the first 3 lines */
             for ($i = 1; $i <= 3; $i++) {
             the_row();
             
             /* as an example, we get a text field from the sub-repeater and print it */
             $sub_repeater = get_sub_field('acf_sub_field'); 

             echo '<ol><li>'.$sub_repeater.'</li></ol>';

			 }
     
           endif;
         
           endwhile;
        
    else :
    
    /*inform the user, if the repeater has no content */
    echo "Sorry, no rows found in the repeater";

    endif;

With this code element you have to add and to format / style the repeater content yourself, for example with a css class. If it is just a list of texts, no problem. If you also have images etc., it becomes more work.

For example, if you want to include also an image from the acf repeater, you can use this code, that you have to put within the repeater loop in the code above:

$thumb = wp_get_attachment_image_src(get_sub_field('acf_image'), 'medium');
$fullsize = wp_get_attachment_image_src(get_sub_field('acf_image'), 'full');
        
        if ($fullsize <> "" &&  $thumb <> "") {
           ?>
           <a href="<?php echo $fullsize[0]; ?>">
           <img src="<?php echo $thumb[0]; ?>"  />
           </a>
           <?php 
      };
1 Like

Hey guys, you can easily use the acf/format_value hook to limit the number of items to output.

/*
 * change "my_repeater" in the hook name
 * to the name of the ACF repeater you want to limit
 */
add_filter( 'acf/format_value/name=my_repeater', function( $value, $post_id, $field ) {
    $max_items = 3; // max number of items you want to output
    return ( count( $value ) > $max_items ) ? array_slice( $value, 0, $max_items ) : $value;
}, 10, 3 );

Backend:

Frontend:

CleanShot 2023-01-29 at 14.44.03

7 Likes