使用ACF输出的嵌套中继器字段分组

问题描述 投票:0回答:1

我正在尝试输出嵌套的ACF转发器字段,以使输出如下:

第1组名称

第1组值数组:

  • 捐赠者名称
  • 公司名称
  • 照片网址

第2组名称

第2组值数组:

  • 捐赠者名称
  • 公司名称
  • 照片网址

到目前为止,我有以下代码:

    $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;

并且我将其输出如下:

<?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; ?>

此输出为我提供1和2的不同组名称,但输出相同的公司名称,照片网址和捐赠者名称信息。理想情况下,我希望设置代码以使其使用与key=>value类似的$donor_photo_urls['group 1'],但是我以前的模仿我在ACF支持论坛上看到的内容的尝试导致NULL输出。

最终,我想为每个组名称打印唯一的数组。感谢任何帮助!

php wordpress advanced-custom-fields
1个回答
0
投票
我假设您的中继器在此处group_donors内设置为sponsor_group作为嵌套中继器。实际上,在中继器上使用get_field()函数将返回一个包含所有子字段和其中的任何中继器的关联数组。 https://www.advancedcustomfields.com/resources/get_field/

因此,您应该能够执行以下操作来输出所有组及其嵌套的供体。

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;

© www.soinside.com 2019 - 2024. All rights reserved.