1

I am trying to output my nested ACF repeater fields such that the output is as follows:

Group 1 Name

Group 1 Array of values:

  • Donor Name
  • Company Name
  • Photo URL

Group 2 Name

Group 2 Array of values:

  • Donor Name
  • Company Name
  • Photo URL

I have the following code so far:

    $sponsor_group_names = array();
    $donor_names_list = array();
    $donor_company_names_list = array();
    $donor_photo_urls = array();

    $donors_group_list = array();
    if (have_rows('sponsor_group')):
        while ( have_rows('sponsor_group')) : the_row();
            $sponsor_group_name = get_sub_field('sponsorship_group_name');
            array_push($sponsor_group_names, $sponsor_group_name);
                if (have_rows('group_donors')):
                    while ( have_rows('group_donors')) : the_row();
                        $donors_group = get_field('group_donors');
                        array_push($donors_group_list, $donors_group);

                        $donor_name = get_sub_field('donor_name');
                        array_push($donor_names_list, $donor_name);
                        $donor_company_name = get_sub_field('donor_company_name');
                        array_push($donor_company_names_list, $donor_company_name);
                        $donor_photo = get_sub_field('donor_photo');
                        array_push($donor_photo_urls, $donor_photo);
                    endwhile;
                endif;
        endwhile;
    endif;

And I am outputting this as follows:

<?php foreach ($sponsor_group_names as $group): ?>
<h2 class="text-primary mb-3">{{$group}}</h2>
    <div class="row" style="margin-bottom: 0 !important">

    <?php
    $iterator = 0;
    foreach ($donor_names_list as $donor): ?>
        <div class="col-md-4 contact-card" style="min-width: 300px;">
            <div class="row" style="margin-bottom: 0 !important">
                <div class="col-lg col-6"><img src="{{$donor_photo_urls[$iterator]}}" class="donor-photo"></div> <!--https://via.placeholder.com/100-->
                <div class="col-lg col-6 donor-info"> <p>{{$donor}} </p> <p> {{$donor_company_names_list[$iterator]}} </p></div>
            </div>
        </div>

    <?php $iterator++;
endforeach; ?>

</div>
<?php endforeach; ?>

This output gives me different Group names for 1 and 2, but outputs the same info for company name, photo url, and donor name. I would ideally like to set up my code so that it uses a key=>value like $donor_photo_urls['group 1'] but my previous attempts at mimicking what I saw on support forums for ACF have resulted in NULL output.

Ultimately I want to print unique arrays for each group name. Appreciate any help!

1
  • Can you post a screengrab (or JSON) of your repeater setup? Commented Jan 27, 2020 at 16:35

2 Answers 2

2

I'm assuming that your repeater is set up having group_donors as a nested repeater within sponsor_group here. Using the get_field() function on a repeater will actually return an associative array with all sub fields and any repeaters within them. https://www.advancedcustomfields.com/resources/get_field/

As such, you should be able to do something like the following to output all of your groups and their nested donors.

foreach (get_field('sponsor_group') as $sponsorGroup) : ?>
    <h2 class="text-primary mb-3"><?= $sponsorGroup['sponsorship_group_name'] ?></h2>
    <div class="row" style="margin-bottom: 0 !important">
        <?php foreach ($sponsorGroup['group_donors'] as $groupDonor) : ?>
            <div class="col-md-4 contact-card" style="min-width: 300px;">
                <div class="row" style="margin-bottom: 0 !important">
                    <div class="col-lg col-6"><img src="<?= $groupDonor['donor_photo'] ?>" class="donor-photo"></div>
                    <div class="col-lg col-6 donor-info">
                        <p><?= $groupDonor['donor_name'] ?></p>
                        <p><?= $groupDonor['donor_company_name'] ?></p>
                    </div>
                </div>
            </div>
        <?php endforeach ?>
    </div>
<?php endforeach;
Sign up to request clarification or add additional context in comments.

Comments

0

<---Working---->

foreach (get_field('sponsor_group') as $sponsorGroup) : ?> " class="donor-photo">

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.