我想循环遍历 WordPress 自定义帖子类型
career
字段 country
所有值并获取每个字段值一次以使用它们进行过滤。它可能应该类似于 i.e:
$field_key = "field_64c18961a77a3";
$field = get_field_object($field_key);
if( $field )
{
echo '<div class="acf-location-values">';
foreach( $field['value'] as $k => $v )
{
echo '<a data-filter=.'.$k.' onclick="return false;">' . $v . '</a>';
}
echo '</div>';
}
插件中的字段设置:
array(
'key' => 'field_64c18961a77a3',
'label' => 'Country',
'name' => 'country',
'aria-label' => '',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'maxlength' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
),
这似乎没有给我预期的结果(
Warning: Invalid argument supplied for foreach()
)-如何实现这一目标?任何建议表示赞赏。
编辑:
$field
的 var_dump:
array(23) { ["ID"]=> int(0) ["key"]=> string(19) "field_64c18961a77a3" ["label"]=> string(7) "Country" ["name"]=> string(7) "country" ["aria-label"]=> string(0) "" ["prefix"]=> string(3) "acf" ["type"]=> string(4) "text" ["value"]=> string(2) "EE" ["menu_order"]=> int(5) ["instructions"]=> string(0) "" ["required"]=> int(0) ["id"]=> string(0) "" ["class"]=> string(0) "" ["conditional_logic"]=> int(0) ["parent"]=> string(19) "group_64c188737eb46" ["wrapper"]=> array(3) { ["width"]=> string(0) "" ["class"]=> string(0) "" ["id"]=> string(0) "" } ["default_value"]=> string(0) "" ["maxlength"]=> string(0) "" ["placeholder"]=> string(0) "" ["prepend"]=> string(0) "" ["append"]=> string(0) "" ["_name"]=> string(7) "country" ["_valid"]=> int(1) }
如果我使用$field['value']
我仍然得到一个无效的参数。我应该将 foreach 放入帖子循环中吗?
$value = get_field('location');
$all_attributes[] = $value;
然后根据 $all_attributes
数组创建一个唯一的数组:
<?php
/* use array_unique to remove duplicates from the array */
$filter_attributes = array_unique ($all_attributes);
/* display the values on the same line, separated by spaces */
echo implode(" ", $filter_attributes ); ?>