我有一本自定义帖子类型的书,在它的参数中我有
'show_in_rest' => true,
现在我想向其中添加一个自定义字段,如下所示:
add_action( 'init', 'add_to_rest_test' );
function add_to_rest_test(){
$meta_args = array(
'type' => 'string',
'description' => 'A meta key associated with a string meta value.',
'single' => true,
'show_in_rest' => true,
);
register_post_meta( 'book', 'testing_field', $meta_args );
}
但是当我转到帖子 json 时:myexamplesite/wp-json/wp/v2/book…。没有“元”部分。但如果我将代码更改为常规帖子:
register_post_meta( 'post', 'testing_field', $meta_args );
在 json 中我确实有一个元部分:
"meta": {
"testing_field": "",
"footnotes": ""
},
我在这里缺少什么?感谢任何帮助
注册
book
时,需要添加'supports' => ['custom-fields']
:
register_post_type( 'book', [
...
'supports' => [ 'title', 'editor', 'custom-fields', ... ],
...
] );