“ product_cat” json中的问题添加字段 按照说明进行操作后,数据的空白字段会出现
add_action('rest_api_init', 'register_custom_fields');
function register_custom_fields(){
register_rest_field(
'product_cat',
'image',
array(
'get_callback' => 'show_fields'
)
);
register_rest_field(
'product_cat',
'color',
array(
'get_callback' => 'show_fields'
)
);
}
function show_fields($object, $field_name, $request){
$field_name = 'wpcf-' . $field_name;
return get_post_meta($object['id'], $field_name, true);
}