我在WordPress主题中有一个自定义帖子类型,我试图使使用该帖子类型创建的每个页面在“高级自定义字段”中作为选择选项出现。
这是我如何使用过滤器将数据加载到自定义字段中的方法:
function acf_custom_field_choices( $field ) {
// reset choices
$field['choices'] = array();
$args = array(
'post_type' => 'custom_post_type',
'parent' => 0
);
$pages = get_pages( $args );
foreach ( $pages as $page ) {
// vars
$value = $page->post_name;
$label = $page->post_title;
// append to choices
$field['choices'][ $value ] = $label;
}
// return the field
return $field;
}
add_filter('acf/load_field/name=custom_field','acf_custom_field_choices', 10, 1);
现在,这为我提供了select
字段中的选项,供ACF查看类似以下项:
blue : Blue
red : Red
green : Green
yellow : Yellow
尝试在主题中显示选择时遇到问题,因为通过unidentified index
错误,这使我无法选择不是选择之一。
这是我尝试显示选择的方式:
$field = get_field_object( 'custom_field' );
$value = get_post_meta( $post_id, 'custom_field', true );
echo $field['choices'][ $value ];
如果我转储$field['choices']
数组,则仅显示添加到该ACF的原始数据,而不显示过滤器中更改的数据,这就是抛出unidentified index
错误的原因。 / p>
是否需要更改某些内容才能正确显示动态加载的字段数据?