【发布时间】:2021-01-04 14:28:30
【问题描述】:
我使用ACF将类别图标添加到类别中作为ACF,并且已经添加成功, 现在我想在 REST API 输出中显示这个字段: 我使用了这段代码,但它不起作用。 get_field 和 get_field_object 只是在 REST API 输出中返回 null(见附图) 这是实时输出: http://134.209.149.245/index.php?rest_route=/wp/v2/categories
function wpse_2879312_register_category_icon_field()
{
register_rest_field(
array('category'),
'category_icon',
array(
'get_callback' => 'wpse_2879312_get_category_icon',
'update_callback' => null,
'schema' => null,
)
);
}
add_action('rest_api_init', 'wpse_2879312_register_category_icon_field');
function wpse_2879312_get_category_icon($object, $field_name, $request)
{
$category_icon = get_field_object('category_icon' , get_the_ID());
return $category_icon ['value'];
}
【问题讨论】:
标签: wordpress advanced-custom-fields wordpress-rest-api