Support

Account

Home Forums Add-ons Repeater Field Nested Repeater to show only one result

Solved

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

  • Thanks Elliot, that worked perfectly.

  • 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

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

The topic ‘Nested Repeater to show only one result’ is closed to new replies.