猿问

使用 ACF 对嵌套中继器字段输出进行分组

我正在尝试输出嵌套的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;


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


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


慕莱坞森
浏览 104回答 1
1回答

一只萌萌小番薯

我假设您的中继器已设置为此处的嵌套中继器。在转发器上使用该函数实际上将返回一个关联数组,其中包含所有子字段及其中的任何转发器。https://www.advancedcustomfields.com/resources/get_field/group_donorssponsor_groupget_field()因此,您应该能够执行以下操作来输出所有组及其嵌套的捐赠者。foreach (get_field('sponsor_group') as $sponsorGroup) : ?>&nbsp; &nbsp; <h2 class="text-primary mb-3"><?= $sponsorGroup['sponsorship_group_name'] ?></h2>&nbsp; &nbsp; <div class="row" style="margin-bottom: 0 !important">&nbsp; &nbsp; &nbsp; &nbsp; <?php foreach ($sponsorGroup['group_donors'] as $groupDonor) : ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col-md-4 contact-card" style="min-width: 300px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="row" style="margin-bottom: 0 !important">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col-lg col-6"><img src="<?= $groupDonor['donor_photo'] ?>" class="donor-photo"></div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col-lg col-6 donor-info">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p><?= $groupDonor['donor_name'] ?></p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p><?= $groupDonor['donor_company_name'] ?></p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; <?php endforeach ?>&nbsp; &nbsp; </div><?php endforeach;
随时随地看视频慕课网APP
我要回答