我有以下字段设置:
welcome_screen (type: group)
title
terms_group (type: group)
terms_text (type: text)
我正在尝试获取terms_text
的值。
到目前为止,我拥有的是:
<?php
$welcome_screen = get_field('welcome_screen'); // type: group
if($welcome_screen):
$title = $welcome_screen['title'];
while( have_rows('welcome_screen') ): the_row();
$terms_group = $welcome_screen('terms_group'); // nested group
$terms_text = $terms_group['terms_text'];
endwhile;
endif;
echo $terms_text;
?>
目前,如果我在while
循环中echo $terms_text
,我会在这行得到错误Function name must be a string
:$terms_group = $welcome_screen('terms_group');
我还想在循环之外使用$terms_text
变量,所以想知道是否有另一种方法可以在没有while循环的情况下实现我想要的目标?
编辑:
我运行了一个var_dump
来检查输出:
$welcome_screen = get_field('welcome_screen'); // type: group
echo '<pre>'; var_dump($welcome_screen); echo '</pre>';
这是输出:
array(3) {
["title"]=>
string(13) "Health Survey"
["standfirst"]=>
string(113) "We just need some answers to some quick health questions about your general health to get you the best treatment."
["terms_group"]=>
array(3) {
["terms_text"]=>
string(41) "By proceeding you agree to the following:"
["terms_listing"]=>
array(2) {
[0]=>
array(1) {
["terms"]=>
string(88) ""
}
[1]=>
array(1) {
["terms"]=>
string(0) ""
}
}
["agree_to_terms_radio"]=>
string(3) "Yes"
}
}
好吧,为发布转储数据而欢呼。
所以我认为你不需要使用循环,你可以试试下面的php。它没有经过测试,所以如果我犯了任何错误,你可能需要修复。
我不记得如果没有设置数组值,acf是否会输出数组值,所以您可能需要将isset()
添加到一些php变量中,以检查是否已设置。
查看代码中的注释。。。
<?php
// get welcome screen group
$welcome_screen = get_field('welcome_screen');
// check if we have group
if($welcome_screen) {
// output the welcome title
echo $welcome_screen['title'];
// set the terms group
$terms_group = $welcome_screen['terms_group'];
// if terms group is array
if(is_array[$terms_group]) {
// output term group term text
echo $terms_group['terms_text'];
// set term group term listing
$terms_listings = $terms_group['terms_listing'];
// if terms listings is an array
if(is_array($terms_listings)) {
// foreach listings
foreach($terms_listings as $terms_listing) {
// output listing terms
echo $terms_listing['terms'];
}
}
// output term group term agreement
echo $terms_group['agree_to_terms_radio'];
}
}