You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress中ACF复杂重复器与组字段循环显示问题求助

解决ACF中嵌套Repeater与Group字段的遍历问题

你的核心问题是遍历嵌套在Group字段里的Repeater字段时,have_rows的上下文参数使用错误。country是Group字段,通过get_sub_field('country')获取到的是关联数组,但你调用have_rows('country_locations', 'country')时传入了字段名'country',不符合ACF的API要求,导致无法进入country_locations的循环。

以下是两种可行的修正方案:

方法一:使用ACF have_rows 保持上下文

<?php if (have_rows('locations')) : ?>
    <?php while (have_rows('locations')) : the_row(); ?>
        <?php 
        $country = get_sub_field('country');
        if (!$country || empty($country['country_name'])) continue;
        ?>
        <div>
            <h2><?php echo esc_html($country['country_name']); ?></h2>
            <?php 
            // 传入$country作为上下文,正确识别嵌套的Repeater字段
            if (have_rows('country_locations', $country)) : ?>
                <?php while (have_rows('country_locations', $country)) : the_row(); ?>
                    <?php 
                    $city = get_sub_field('city');
                    if (!$city) continue;
                    ?>
                    <div>
                        <h4><?php echo esc_html($city['city_name']); ?></h4>
                        <h5><?php echo esc_html($city['city_company_name']); ?></h5>

                        <?php if (!empty($city['city_image'])) : ?>
                            <div>
                                <img src="<?php echo esc_url($city['city_image']['url']); ?>" 
                                     alt="<?php echo esc_attr($city['city_image']['alt']); ?>" />
                            </div>
                        <?php endif; ?>
                    </div>
                <?php endwhile; ?>
            <?php endif; ?>
        </div>
    <?php endwhile; ?>
<?php endif; ?>

说明:将have_rows的第二个参数改为$country(从get_sub_field获取的Group数组),让ACF正确识别嵌套字段的上下文;同时添加esc_html/esc_url等安全输出函数,避免XSS风险。

方法二:直接遍历数组(更直观)

Group字段返回关联数组,嵌套的Repeater字段也是数组形式,可直接循环操作:

<?php if (have_rows('locations')) : ?>
    <?php while (have_rows('locations')) : the_row(); ?>
        <?php 
        $country = get_sub_field('country');
        if (!$country || empty($country['country_name'])) continue;
        ?>
        <div>
            <h2><?php echo esc_html($country['country_name']); ?></h2>
            <?php 
            $country_locations = $country['country_locations'];
            if (!empty($country_locations) && is_array($country_locations)) : ?>
                <?php foreach ($country_locations as $location) : ?>
                    <?php 
                    $city = $location['city'];
                    if (!$city) continue;
                    ?>
                    <div>
                        <h4><?php echo esc_html($city['city_name']); ?></h4>
                        <h5><?php echo esc_html($city['city_company_name']); ?></h5>

                        <?php if (!empty($city['city_image'])) : ?>
                            <div>
                                <img src="<?php echo esc_url($city['city_image']['url']); ?>" 
                                     alt="<?php echo esc_attr($city['city_image']['alt']); ?>" />
                            </div>
                        <?php endif; ?>
                    </div>
                <?php endforeach; ?>
            <?php endif; ?>
        </div>
    <?php endwhile; ?>
<?php endif; ?>

说明:跳过have_rows的上下文处理,直接操作数组,逻辑更清晰,适合嵌套层级较多的场景;需提前判断数组是否存在且非空,避免报错。

内容的提问来源于stack exchange,提问作者Maciej W.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 19:05:24