Home › Forums › Add-ons › Repeater Field › Nested Repeater to show only one result
I am trying to get the nested repeater to show only the first result. I have seen the code for a general repeater set-up but not on nested repeaters.
The following code takes looks at ACF fields from a different page, and will provide multiple results, and it works.
I need it to stop at the first result rather than looping through all the results.
I have tried all the code variations I can think off but still no joy.
Any ideas?
<?php if( get_field('project', $pid) ): ?>
<?php while( has_sub_field('project', $pid) ): ?>
<?php if(get_sub_field('project_name') == $pn ) : ?>
<?php if( get_sub_field('project_images') ): ?>
<?php while( has_sub_field('project_images') ): ?>
<?php $firstimage = wp_get_attachment_image_src(get_sub_field('project_image'), 'full'); ?>
<img src="<?php echo $firstimage[0]; ?>" alt="" style="display: none;" class="fp_preview" />
<?php endwhile; ?>
<?php endif; ?>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>

Hi @GavinA1970
The simpleset way is to break the while loop after you render the image like so:
<?php while( has_sub_field('project_images') ): ?>
<?php $firstimage = wp_get_attachment_image_src(get_sub_field('project_image'), 'full'); ?>
<img src="<?php echo $firstimage[0]; ?>" alt="" style="display: none;" class="fp_preview" />
<?php break; ?>
<?php endwhile; ?>
That will break out of the nested loop and continue the previous loop.
Hope that helps.
Thanks
E
Does anyone know how I would apply this to breaking a nested repeater after the SECOND result?

Hi @lsterling03
You can easily use a ‘counter’ variable, and call the break function when the counter hits 2.
Thanks
E
The topic ‘Nested Repeater to show only one result’ 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.