ACF Wordpress 组,中继器内有中继器

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

我有一个中继器,其中中继器是一个组,该组内有另一个中继器。
现在我想从组内的中继器获取值。

这是我的结构:

-partner (repeater)

--info (group)
---logo
---headline
---subline
---description
---facts (repeater)
----fact

这就是我的代码的样子:

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

<div>
    <?php while( have_rows('partner')): the_row();
    $info = get_sub_field('info);
    ?>

    Here is some output of the Group $info that works perfectly..

        <?php while( have_rows('info')): the_row(); ?>
            <?php while(have_rows('facts')): the_row();
            $fact = get_sub_field('fact');
                <?php print_r($fact) ?> <--- No outcome!
            <?php endwhile; ?>
        <?php endwhile;?>

    <?php endwhile; ?>
</div>

已经在网上搜索了..这就是我找到的东西..但仍然不起作用。

php html wordpress advanced-custom-fields
1个回答
0
投票

我调整了您的

PHP
代码以正确循环遍历字段。首先,我使用 foreach 循环来迭代顶级“
partner
”重复器字段。在每个“
partner
”项目中,我访问“
info
”组并提取其字段。然后,使用另一个
foreach
循环来迭代“info”组内的“facts”重复器字段。 试试这个

<?php 
$partners = get_field('partner');  
if($partners):
    foreach($partners as $partner):
        $info = $partner['info'];  // 'info' is a group inside the 'partner' repeater

        // Output some fields from the 'info' group
        echo $info['headline'];
        echo $info['subline'];
        // ... other fields from 'info'

        // Now loop through 'facts' repeater inside 'info' group
        if($info['facts']):
            foreach($info['facts'] as $fact):
                echo $fact['fact'];  // Output the 'fact' field inside the 'facts' repeater
            endforeach;
        endif;
    endforeach;
endif;
?>

<div>
    <!-- Your HTML and output here -->
</div>
© www.soinside.com 2019 - 2024. All rights reserved.