【发布时间】:2021-04-08 11:39:39
【问题描述】:
我有以下字段设置:
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"
}
}
【问题讨论】:
-
请在
$welcome_screen = get_field('welcome_screen');之后添加echo '<pre>'; var_dump($welcome_screen); echo '</pre>';并分享输出的转储数组。 -
嗨@joshmoto - 当然:) 我已经编辑了我的问题以展示输出
-
为什么不正常循环遍历数组?
-
如果您试图在
while循环之外获取$terms_text,似乎没有理由在这里使用循环。为什么不使用数组键直接访问数据?看来您甚至没有使用循环来迭代多个数组。
标签: php wordpress advanced-custom-fields