我有一个名为“course\\u location”的CPT字段作为带有值的复选框。当我使用
$field = get_field_object(\'course_location\');
\'<pre>searchDateArray\'; print_r($field); echo \'</pre>\';
从而生成以下数组
Array (
[key] => field_56056d11c5dd6
[label] => Course Location
[name] => course_location
[_name] => course_location
[type] => checkbox
[order_no] => 3
[instructions] =>
[required] => 0
[id] => acf-field-course_location
[class] => checkbox
[conditional_logic] => Array (
[status] => 0
[rules] => Array (
[0] => Array (
[field] => null
[operator] => ==
)
)
[allorany] => all
)
[choices] => Array (
[orange-county] => Orange County
[los-angeles-county] => Los Angeles County
[test] => Test
)
[default_value] =>
[layout] => vertical
[field_group] => 29
[value] => Array (
[0] => test
)
()
我如何获取[选项]数组并遍历“选项”并进行回显?
<li>$label and $name</li>
输出:orange county和orange county
输出:洛杉矶县和洛杉矶县
输出:测试和测试
最合适的回答,由SO网友:jdm2112 整理而成
这将使您获得内部的选项数组$field
变量:
$choices = $field[ \'choices\' ];
从那里,您可以使用while或foreach循环进行迭代,这取决于您试图对数组执行的操作。
编辑:重读你的问题,你可以指定你想做什么!抱歉跳过了这部分!
echo \'<ul>\';
foreach($choices as $key => $choice):
echo \'<li>\' . $key . \' and \' . $choice . \'</li>\';
endforeach;
echo \'</ul>\';
应产生:
这不应被视为完整的代码。出于安全考虑,请退出输出。这里只是说明一个概念。