Support

Account

Home Forums General Issues Loop Through Nested Repeater Fields

Solved

Loop Through Nested Repeater Fields

  • I’ve looked on the Working with Nested Repeaters page, but can’t see any mention of how to achieve this.

    Basically, I have multiple child Repeater Field’s (wash, bleach …) inside a parent Repeater Field (care_labels). I’m trying to loop through this parent repeater and retrieve all the child repeater fields and their values, all in a neat unordered list.

    So far my code looks like this:

    <p>Care Labels:</p>
    
    <ul>
    
    	<?php if( have_rows('care_labels') ): ?>
    
    		<?php while( have_rows('care_labels') ): the_row();
    
    			while( have_rows('wash') ): the_row(); ?>
    				<li><?php echo get_sub_field('instructions') . '&' . get_sub_field('finish'); ?></li>
    			<?php endwhile; ?>
    
    	<?php endwhile; ?>
    
    </ul>
    
    <?php endif; ?>

    The problem I’m having is my while loop is targeting the ‘wash’ repeater. Now I could repeat that while loop five times, but that seems like an awful lot of bloat. I was wondering if anyone could point me in the direction of a more elegant solution?

    I was thinking of storing my child repeater field names in an array, but it’s working out how to loop through these with the have_rows function.

    Here are my fields in action, if my above explanation sounds too confusing.

    labels

    Any help is appreciated. Thanks in advance!

  • Hi @realph

    Thanks for the screenshot and info, but I don’t understand what is and isn’t working.

    What are you having trouble doing?
    Does your loop work?

    Thanks
    E

  • Sorry for the late reply, I thought I had email notifications set up for this post.

    So the loop is working, but because I’m doing have_rows('wash') it’s only looping through my ‘wash’ rows. I would like it to loop through every single repeater row, of which I have 4 (wash, tumble, iron and dry).

    I can achieve this by doing 4 while loops, but figured there might be a more elegant solution out there that uses 1 while loop and jumps through my rows.

  • Hi @realph

    Thanks for the follow up.

    You will need to write 4 while loops because ACF requires specific field names to know what data to iterate through.

    Thanks
    E

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

The topic ‘Loop Through Nested Repeater Fields’ is closed to new replies.