我想将自定义字段注册到Wordpress Rest API。到目前为止,我在此消息的底部有代码,用于注册一个字段。
如何在单个密钥下注册/嵌套多个自定义字段?
我希望我的JSON看起来像这样,“location”键下的所有内容都是自定义字段,从我的自定义字段数据中返回这些值:
"location" : {
"mapZoom":
"mapLat":
"mapLng":
"markerLat":
"markerLng":
"addressTitle":
"address":
"city":
"state":
"addressCountry":
"geoJson": [ "mapLng", "mapLat" ]
},
这是我注册单键/值对的代码。
add_action('rest_api_init', 'register_custom_fields');
function register_custom_fields(){
register_rest_field(
'post',
'formatted',
array(
'get_callback' => 'show_fields'
)
);
}
function show_fields($object, $field_name, $request){
return get_post_meta($object['id'], $field_name, true);
}