Search code examples
phphtmlwordpressadvanced-custom-fields

ACF Wordpress Group with Repeater inside a Repeater


i have a Repeater and in that Repeater is a Group and inside this Group is another Repeater.
Now i want to get the values from the Repeater inside the Group.

This is my Structure:

-partner (repeater)

--info (group)
---logo
---headline
---subline
---description
---facts (repeater)
----fact

And this is how my Code Kinda looks like:

<?php 
$partner = get_sub_field('partner');
?>

<div>
    <?php while( have_rows('partner')): the_row();
    $info = get_sub_field('info);
    ?>

    Here is some output of the Group $info that works perfectly..

        <?php while( have_rows('info')): the_row(); ?>
            <?php while(have_rows('facts')): the_row();
            $fact = get_sub_field('fact');
                <?php print_r($fact) ?> <--- No outcome!
            <?php endwhile; ?>
        <?php endwhile;?>

    <?php endwhile; ?>
</div>

Already searched the Web.. that was the stuff i kinda found.. but still not working.


Solution

  • I adjusted your PHP code to correctly loop through the fields. First, I used a foreach loop to iterate over the top-level 'partner' repeater field. Within each 'partner' item, I accessed the 'info' group and extracted its fields. Then, another foreach loop was used to iterate over the 'facts' repeater field inside the 'info' group. Try this

    <?php 
    $partners = get_field('partner');  
    if($partners):
        foreach($partners as $partner):
            $info = $partner['info'];  // 'info' is a group inside the 'partner' repeater
    
            // Output some fields from the 'info' group
            echo $info['headline'];
            echo $info['subline'];
            // ... other fields from 'info'
    
            // Now loop through 'facts' repeater inside 'info' group
            if($info['facts']):
                foreach($info['facts'] as $fact):
                    echo $fact['fact'];  // Output the 'fact' field inside the 'facts' repeater
                endforeach;
            endif;
        endforeach;
    endif;
    ?>
    
    <div>
        <!-- Your HTML and output here -->
    </div>