ACF嵌套组字段获取参数

时间:2018-08-09 00:48:52

标签: wordpress nested advanced-custom-fields

我已经尝试过使用嵌套组的不同变体,但仍然无法使其正常工作。这就是我所拥有的:
        组(组)
           -组标题(文字)
           -子组(组)
           --- **子组标题(文本)
           --- **子组文本(textarea)
我只能获得组标题。不是子组。我的代码:

<div class="test">
            <?php
                $group = get_field('group');
                echo $group['group_title'];
                if( get_field($group) ):
                    if(get_sub_field('sub_group')):
                        echo the_sub_field('sub_group_title');        
                        echo the_sub_field('sub_group_text');
                    endif;
                endif;

            ?>
</div>

我是否需要在此处使用数组来获取sub_group_title和sub_group_text的字段?
已更新:

<div class="test">
<?php
    if( have_rows('group') ):
      while( have_rows('group') ): the_row();
        $group_title = get_sub_field('group_title');
        $sub_group = get_sub_field('sub_group');
      endwhile;
    endif;
    echo '<p>' . $group_title . '</p>';
    echo '<p>' . $sub_group['sub_group_title'] . '</p>';
    echo '<p>' . $sub_group['sub_group_text'] . '</p>';
?>

1 个答案:

答案 0 :(得分:1)

我认为您需要在打印sub_fields之前添加一会儿

while( have_rows('group') ): the_row(); 
   echo the_sub_field('sub_group_title');        
endwhile;

在这里您需要将if(get_field($ group)):更改为if($ group):因为您已经在$ group = get_field('group');中分配了该值

更多示例在这里: https://www.advancedcustomfields.com/resources/group/

希望有帮助!

相关问题