我正在尝试获取自定义帖子类型的类别但无法找到任何解决方案。 The WP REST API documentation仅返回博文的类别。
自定义帖子API:https://themographics.com/wordpress/service-providers/wp-json/wp/v2/sp_categories
类别API:https://themographics.com/wordpress/service-providers/wp-json/wp/v2/categories [返回博客帖子类型的默认类别]
有什么办法吗? https://themographics.com/wordpress/service-providers/wp-json/wp/v2/sp_categories/categories
答案 0 :(得分:1)
如果仍在寻找解决方案:
全部获取:-Passing arguments to template files in WordPress 5.5
单身:-https://yoursite.com/wp-json/wp/v2/your-taxonomy
被子弹击中:-https://yoursite.com/wp-json/wp/v2/your-taxonomy/id
在我的情况下,我使用了
https://yoursite.com/wp-json/wp/v2/your-taxonomy?slug=slugname
https://app.local/wp-json/wp/v2/works-categories
https://app.local/wp-json/wp/v2/works-categories/5
来源:https://app.local/wp-json/wp/v2/works-categories?slug=my-slug-name
答案 1 :(得分:0)
你可以这样做:
add_action('init', 'json_handler');
function json_handler(){
$categories = get_terms( 'my_cat', 'orderby=count&hide_empty=0' );
if( ! is_wp_error( $categories ) ) {
// encode the $categories array as json
print_r( json_encode( $categories ) );
}
}