Home Forums Add-ons Repeater Field Limit nested repeater results to 2


Limit nested repeater results to 2

  • I need to limit a nested repeater to 2 results. I found an answer for how to do this for a regular repeater, but on a nested repeater, it breaks the parent repeater as well. Here is my code. How can I modify this to only break the child repeater?

     <?php if(get_field('speaker_bio')): ?>
                <ul class="speaker-grid clearfix">
                	<?php while(has_sub_field('speaker_bio')): ?>	
          			<li class="speaker">
                    	<div class="headshot">
                        	<?php if(get_sub_field('social_networks')): $i=0; ?>
                          	<div class="social-hover">
                                <?php while(has_sub_field('social_networks')): 
                                 $i++; if( $i > 2 ) { break; } ?>	
                                       <a href="<?php the_sub_field('url'); ?>" class="<?php the_sub_field('network'); ?>" target="_blank">Facebook</a>
                                <?php endwhile; ?>           
                            <?php endif; // social_network ?>
                            <?php echo wp_get_attachment_image(get_sub_field('photo'), 'speaker', true); ?>
                    <?php endwhile ?>
              <?php endif; ?>
  • Hi @lsterling03,

    Looking at the code it does not look like nested repeaters are supported because both the has_sub_field and get_sub_field functions use the same ACF global $GLOBALS['acf_field'].

    Nesting them will cause unwanted side affects. So, your inner break is working correctly but pointer in the global will have moved to the end, thus exiting the outer loop too.

  • Thank you. Unfortunately I’m having trouble understanding what you mean by using the same ACF global. Are you saying that it is not possible to break the inner repeater in a nested configuration? Or are you saying I need to change something to make it work?

    Thank you.

  • I found this topic on breaking a nested repeater after the first one. But I’m not sure how to apply this solution to two.

  • Hi @lsterling03,

    The problem is that the same global variable is being used by both loops (inner and outer) so that when your inner loop breaks or ends, the outer loops pointer no longer points to the required field in the array.

    AS stated above Nested Repeaters are not supported, and would require you to do some coding that is unfortunately, outside the scope of this support forum.


  • Hi @lsterling03

    There is some smart logic in the has_sub_field function that will look for a change in field name, and then move up or down a level.

    Therefore, it is possible to break out of the second loop and continue the first.

    Your code looks fine, so I’m not 100% sure why it isn’t working. What version of ACF are you using?


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

The topic ‘Limit nested repeater results to 2’ is closed to new replies.