将ACF字段存储在变量

时间:2016-11-23 17:57:22

标签: wordpress

我使用高级自定义字段创建WP页面。 我有一个由2个字段组成的字段组:

标题 - 文本字段 Columnsection - 包含2个WYSIWYG字段的repeaterfield,名为left_col和right_col。

所以结果可能如下:

Cars (title)
image of a volvo (left_col)        text about a volvo(right_col)
image of a BMW (left_col)          text about a BMW(right_col)
image of a Fiat (left_col)         text about a Fiat(right_col)

好的,我在模板中尝试做的是创建三个包含所有值的变量:

$titles = all Titles
$left_col = all left_cols
$right_col = all right_cols

字段组称为repeater,所以我可以这样做:

<?php
 while( have_rows('repeater') ): the_row();                                 
        $title = get_sub_field('main_title');
        $rows = get_sub_field( 'column_section' );
        $left = $rows['left_column'];
        $right = $rows['right_column'];
        echo $title; // Title echoes out
        echo $left; // nothing (expecting array of left_cols 'related to the title')
        echo $right; //nothing (expecting array of right_cols 'related to the title')
        ?>

我试图这样做的原因是我希望将right_columns和left_columns分开,以便在模板的不同位置使用它们。

帮助表示感谢。谢谢!

1 个答案:

答案 0 :(得分:0)

您需要为嵌套转发器使用正确的语法。

<?php
while ( have_rows('repeater') ): the_row();                                 
    $title = get_sub_field('main_title');
    echo $title; // Title echoes out

    while ( have_rows( 'column_section') ) : the_row();

        $left = get_sub_field( 'left_column ');
        $right = get_sub_field( 'right_column ');

        echo $left; // nothing (expecting array of left_cols 'related to the title')
        echo $right; //nothing (expecting array of right_cols 'related to the title')

    endwhile;

endwhile;

您可以在此处查看官方文档: https://www.advancedcustomfields.com/resources/working-with-nested-repeaters/