【问题标题】:How to get categories of custom post type in WP-JSON v2 API?如何在 WP-JSON v2 API 中获取自定义帖子类型的类别?
【发布时间】:2017-12-06 07:37:12
【问题描述】:

我正在尝试获取自定义帖子类型的类别,但找不到任何解决方案。 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

【问题讨论】:

  • 你能添加你用来获取它们的代码吗?
  • @NarekZakarian 我已经添加了链接,还没有代码,想确认一下 WordPress 是否支持这个?
  • 您可以使用 get_terms() 函数获取自定义帖子类型分类法
  • 我需要在 WP-JSON API 中使用移动应用程序
  • 好的,我会写答案

标签: wordpress wordpress-rest-api


【解决方案1】:
【解决方案2】:

你可以这样做:

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 ) );
    }
}

【讨论】:

  • 这会将所有数据推送到json,而不是您可以自己创建url
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-12
  • 1970-01-01
  • 1970-01-01
  • 2018-06-24
  • 1970-01-01
  • 2015-10-11
相关资源
最近更新 更多