Search code examples
phpwordpressgroupingadvanced-custom-fieldsrepeater

Advanced Custom Fields within WordPress - Unable to use a repeater within a group


I have done this 20+ times previously but I cannot figure out why my call to the repeater field is breaking my site.

The page is unable to load due to a php error, it works if I remove the while loop that is calling the repeater field.

I would like to know what I am doing wrong in this snippet:

~ The repeater field is called application_sectors, might be a good place to start looking for a fix!

<?php
$has_applications_of_ebflow = get_field('has_applications_of_ebflow');
?>

<?php if ( $has_applications_of_ebflow === TRUE ): ?>

    <div class="applications-of-ebflow">

        <div class="applications-of-ebflow-inner">

            <?php
            $applications_of_ebflow = get_field('applications_of_ebflow');
            while ( have_rows('applications_of_ebflow') ): the_row();
                $heading = get_sub_field('heading');
                $application_sectors = get_sub_field('application_sectors');
                $enquiry_link = get_sub_field('enquiry_link');
            ?>

                <?php if ( $heading != '' ): ?><h2 class="aoe-heading"><?php echo $heading; ?></h2><?php endif; ?>

                <?php if ( $application_sectors != '') : ?>

                    <div class="aoe-application-sectors">

                        <?php
                        while ( have_rows('application_sectors') ): the_row();
                            $image = get_sub_field('image');
                            $heading = get_sub_field('heading');
                            $sector_applications = get_sub_field('sector_applications');
                        ?>

                            <div class="aoe-application-sector">

                                <?php if ( $image != '' ): ?>

                                    <div class="aoe-application-sector-image-contain">

                                        <img class="aoe-application-sector-image" alt="sector image" src="<?php echo $image; ?>">

                                    </div>

                                <?php endif; ?>

                                <div class="aoe-application-sector-content">

                                    <?php if ( $heading != '' ): ?>

                                        <h3 class="aoe-sector-heading"><?php echo $heading; ?></h3>

                                    <?php endif; ?>

                                    <?php if ( $sector_applications != '' ): ?>

                                        <div class="sector-applications">

                                            <?php
                                            while ( have_rows('application_sectors') ): the_row();
                                                $application = get_sub_field('application');
                                            ?>

                                                <div class="sector-application">

                                                    <?php echo $application; ?>

                                                </div>

                                            <?php endwhile; ?>

                                        </div>

                                    <?php endif; ?>

                                </div>

                            </div>

                        <?php endwhile; ?>

                    </div>

                <?php endif; ?>

                <?php if ( $enquiry_link != '' ): ?>

                    <div class="aoe-enquiry">
                        <a href="<?php echo $enquiry_link; ?>" class="box-link"></a>
                        <div class="enquiry-text">Enquire about your application</div>
                    </div>

                <?php endif; ?>

            <?php endwhile; ?>

        </div>

    </div>

<?php endif; ?>

I can also attach a screen grab of my ACFs:

Image of my fields within the back-end

I'm fairly sure I am calling my repeater field incorrectly but can't remember how I have got around this situation before.


Solution

  • Instead of this

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

    Use this

    while ( have_rows('sector_applications') ): the_row();
    

    And then call your sub_fields. It's a an repeater inside an repeater. Like a foreach loop inside a foreach. You called it like it's an field but actualy has rows.

    have_rows() – allows us to loop through the available rows in a repeater / flexible content field
    get_sub_field() – returns the value of a sub field (from the current repeater in “has_sub_field”)
    the_sub_field() – displays the value of a sub field (from the current repeater in “has_sub_field”)
    

    More information and an good example you can find in ACF | Working with Nested Repeaters

    I think the problem is here, I have modified your part of the code

    <?php
    while ( have_rows('application_sectors') ): the_row();
    $image = get_sub_field('image');
    $heading = get_sub_field('heading');
    
    while ( have_rows('sector_applications') ): the_row();
    ?>
    

    Your structue is this: Repeater

    • sub_fields (a.k.a get_field)

    But you don't get into another repeater...

    In my example you have

    Repeater

    • sub_fields (a.k.a get_field)
    • repeater inside repeater
      • then you can again the sub_fields from this part of the repeater