Support

Account

Home Forums ACF PRO A group inside a repeater inside a group inside a repeater

Helping

A group inside a repeater inside a group inside a repeater

  • I am having trouble getting the values of the galleries, which are inside a group, which is inside a repeater, which is inside a group, which is inside a repeater. Everything works great if I move the gallery out out of group_parent to be the same level as that group, but I need it inside. So I think my error is in how I am calling that 2nd group. I’ve tried different things, looking at the resource pages the whole time, but still am stuck. Here is the code:

    <?php
    // Parent Repeater
    if( have_rows('parent_gallery') ):
        // Loop through rows.
        while( have_rows('parent_gallery') ) : the_row(); 
            // parent group, nested in parent repeater
            $group_parent = get_sub_field('group_parent');
    
            if ($group_parent): 
                // Repeater inside group
                if( have_rows('children_galleries') ):
                    // Loop through rows.
                    while( have_rows('children_galleries') ) : the_row();
    
                    $gallerySizeSelect = get_sub_field('gallery_size');
                    // $title_group = get_sub_field('title_group');
                 
                    // Group inside nested repeater
                    $group = get_sub_field('images_group');
                    // Get galleries
                    $images = $group['gallery'];
                    if( $group): ?>
                        <div class="images_group">
                            <?php
                            if( $images ): ?>
                                <ul class="dev-gallery">
                                    <?php foreach( $images as $image ): ?>
                                        <li>
                                            <img src="<?php echo esc_url($image['sizes']['thumbnail']); ?>" alt="<?php echo esc_attr($image['alt']); ?>" />
                                        </li>
                                    <?php endforeach; ?>
                                </ul>
                            <?php endif; ?>
                        </div>
                        <?php endif;
                    endwhile;
                endif;
            endif; 
        endwhile;
    endif; ?>
    

    Thanks!

  • You cannot mix get_sub_field() and have_rows().

    
    $group_parent = get_sub_field('group_parent');
    

    gets an array, including the repeater sub field and its sub fields.
    Once you do this then you cannot use have_rows() on the sub fields of this field because you are already inside a have_rows() loop for ‘group_parent’

    basically you either need to loop over the array returned without using have_rows() or you always need to use have_rows(). You cannot loop over a sub fields that is a repeater or a group without looping over the parent repeater or group.

    
    // have rows
    while( have_rows('parent_gallery') ) ....
      while (have_rows('group_parent')) .....
        while( have_rows('children_galleries') ) .....
    
    
    // using the array, I'm not real sure about this but hopefully you get the idea
    while( have_rows('parent_gallery') ) : the_row();
      $group_parent = get_sub_field('group_parent');
        ....
          $children_galleries = $group_parent['children_galleries'];
          foreach ($children_galleries as $gallery)
    

    at any rate, if you output the array you’ll see what you’re working with

    
    $group_parent = get_sub_field('group_parent');
    echo '<pre>'; print_r($group_parent); echo '</pre>';
    

    at each nested level you have a choice, get the sub fields contents or use another have_rows() loop. If you get the sub field values then from that point on you must use the array that is returned and you can no longer use have_rows() for any of its sub fields that are repeaters or groups.

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

You must be logged in to reply to this topic.