admin管理员组文章数量:1315792
Using the Wordpress REST API, I retrieve all my posts into a Angular 6 service. The category field in the JSON displays the ID of the category as an array of numbers.
"category": [ 6 ],
Is there a way, hopefully on the Wordpress end, to have the API use the category name instead? Or add another node to the JSON?
Using the Wordpress REST API, I retrieve all my posts into a Angular 6 service. The category field in the JSON displays the ID of the category as an array of numbers.
"category": [ 6 ],
Is there a way, hopefully on the Wordpress end, to have the API use the category name instead? Or add another node to the JSON?
Share Improve this question asked Jul 25, 2018 at 13:02 SteveSteve 3139 silver badges21 bronze badges 2- Have you read the official REST API Handbook yet? The linked resource covers exactly this – kero Commented Jul 25, 2018 at 13:17
- Doing this as literally requested would cripple existing code that uses the REST API as it expects IDs it can then pass to the category endpoint – Tom J Nowell ♦ Commented Nov 24, 2020 at 0:25
1 Answer
Reset to default -1For my needs, I customized the wp rest posts callback:
function get_all_posts( $data, $post, $context ) {
return [
'id' => $data->data['id'],
'date' => $data->data['date'],
'date_gmt' => $data->data['date_gmt'],
'modified' => $data->data['modified'],
'title' => $data->data['title']['rendered'],
'content' => $data->data['content']['rendered'],
'excerpt' => $data->data['excerpt']['rendered'],
'category' => get_the_category_by_ID( $data->data['categories'][0] ),
'link' => $data->data['link'],
];
}
add_filter( 'rest_prepare_post', 'get_all_posts', 10, 3 );
Category endpoint returns directly the name of the post category.
本文标签: categoriesREST API Display Category names in JSON
版权声明:本文标题:categories - REST API: Display Category names in JSON? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741969022a2407720.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论