我正在尝试通过调用自定义的Wordpress rest API获取所有类别

时间:2018-12-28 11:40:27

标签: wordpress categories wordpress-rest-api

我正在尝试通过在android应用中调用Wordpress rest api来获取所有类别。 http://demo.wp-api.org/wp-json/wp/v2/categories 我可以通过调用上面的URL来获取所有类别,但是我已经安装了类别图像插件,因此除了默认架构之外,我还需要获取每个类别图像。 因此,我必须创建自己的自定义rest api端点。 以下是我的代码。

但是Wordpress函数get_categories返回null。 有人让我知道我的代码有什么错误吗? 为什么我不能得到想要得到的东西? 我尝试在自定义终结点函数中调用get_posts函数,但它也返回null。 那么,此发布的主要目的是为什么不能在我的自定义终结点函数中调用Wordpress函数get_categories和get_posts呢?

register_rest_route($this->namespace, '/'.$this->rest_base, array('methods' => 'GET', 'callback' => array($this, 'get_main_categories')));
...
public function get_main_categories($request) {
$data = get_categories();
$data = rest_ensure_response($data);
return $data;
}

2 个答案:

答案 0 :(得分:0)

好的。 我有原因。 函数get_categories仅返回类别列表,该类别列表中还有一个过帐。

答案 1 :(得分:0)

如果您的类别少于或等于100,请使用:

http://demo.wp-api.org/wp-json/wp/v2/categories?per_page=100

能否给我提供插件名称(类别图片插件的全名或链接是什么)?