wordpress rest api v2 如何列出分类术语?

wordpress rest api v2 how to list taxonomy terms?

我是 v2 的新手,我使用 v1 很长时间,目前正在升级到 v2,我试图让所有术语都属于特定的自定义分类法。

在 v1 中我可以这样做来获取条款 /taxonomies/location_category/terms

但在 v2 中我尝试 /taxonomies/terms 它 return json 错误 "code":"rest_no_route","message":"No route was found matching the URL and request method","data":{"status" :404}}

如果 /taxonomies/location_category/ 它没有显示任何属于分类的术语。

我在google上搜索了几个小时没有显示任何结果,请任何人提供帮助,谢谢

对于自定义分类,be sure that you're setting the 'show_in_rest' argument to be true (default is false) in your register_taxonomy() call

register_taxonomy() 调用也是您可以设置 'rest_base' 参数的地方(默认为分类法名称,在您的示例中为 /location_category/)。

结束在这里编写自定义代码

添加打击代码到functions.php

  class all_terms
{
    public function __construct()
    {
        $version = '2';
        $namespace = 'wp/v' . $version;
        $base = 'all-terms';
        register_rest_route($namespace, '/' . $base, array(
            'methods' => 'GET',
            'callback' => array($this, 'get_all_terms'),
        ));
    }

    public function get_all_terms($object)
    {
        $return = array();
        // $return['categories'] = get_terms('category');
 //        $return['tags'] = get_terms('post_tag');
        // Get taxonomies
        $args = array(
            'public' => true,
            '_builtin' => false
        );
        $output = 'names'; // or objects
        $operator = 'and'; // 'and' or 'or'
        $taxonomies = get_taxonomies($args, $output, $operator);
        foreach ($taxonomies as $key => $taxonomy_name) {
            if($taxonomy_name = $_GET['term']){
            $return = get_terms($taxonomy_name);
        }
        }
        return new WP_REST_Response($return, 200);
    }
}

add_action('rest_api_init', function () {
    $all_terms = new all_terms;
});

并输入 url http://youdomain.com/wp-json/wp/v2/all-terms?term=you_taxonomy

so term = you_taxonomy, 将得到属于 job_category.

的术语

接受的答案大部分对我有用。这就是我得到的

<?php
// your_theme/functions.php
/**
 * how to list all taxonomy terms
 */
class all_terms
{   
    public function __construct()
    {   
        $version = '2';
        $namespace = 'wp/v' . $version;
        $base = 'all-terms';
        register_rest_route($namespace, '/' . $base, array(
            'methods' => 'GET',
            'callback' => array($this, 'get_all_terms'),
        )); 
    }   
    public function get_all_terms($object)
    {   
        $args = array(
            'public' => true,
            '_builtin' => false
        );  
        $output = 'names'; // or objects
        $operator = 'and'; // 'and' or 'or' 
        $taxonomies = get_taxonomies($args, $output, $operator);
        foreach ($taxonomies as $key => $taxonomy_name) {
            if ($taxonomy_name = $_GET['term']) {
                $return[] = get_terms(array(
                    'taxonomy' => $taxonomy_name,
                    'hide_empty' => false,
                )); 
            }   
        }   
        return new WP_REST_Response($return, 200);
    }   
}
add_action( 'rest_api_init', get_all_terms);
?>

与文档更匹配https://developer.wordpress.org/reference/functions/get_terms/

如果以后有人读到这篇文章,我会 运行 解决默认 WP 类别为每个术语对象输出父键 0、1、2 等的问题,这是一个问题本身,但当自定义分类法在对象上没有此父值时会出现更大的问题

要解决此问题,请修改打勾的示例:

    foreach ($taxonomies as $key => $taxonomy_name) {
        if($taxonomy_name = $_GET['term']){
            $return = get_terms( array( 
                'taxonomy' => $taxonomy_name,
                'hide_empty' => false,
            ));
        }
    }

分类术语的简称是这样的:

https://yoursite.com/wp-json/wp/v2/the-taxonomy-slug

例如,回答你的问题:

https://yoursite.com/wp-json/wp/v2/location_category

来自终端:

curl -X GET -i http://www.example.com/wp-json/wp/v2/location_category

有些开发者甚至对我来说似乎有些困惑。

正确的 URL 是 https://example.com/wp-json/wp/v2/{your_taxonomy}

没有 https://example.com/wp-json/wp/v2/taxonomies/{your_taxonomy}

不需要“/taxonomies”

TLDR

注册分类时使用标志 show_in_rest。就这些了。

详情

列出所有可用的分类法

所有默认分类法都可以通过 REST API 获得。使用以下端点获取所有可用分类的列表:

https://exmaple.org/wp-json/wp/v2/taxonomies

它将在 wp:items 标签中告诉您每个分类的正确端点,例如

..., "wp:items":[{"href":"https://example.com/wp-json/wp/v2/categories"}], ...
..., "wp:items":[{"href":"https://example.com/wp-json/wp/v2/tags"}], ...

向 REST 端点添加新分类法

如果您的分类未在此分类概述中列出,您需要在调用 register_taxonomy 时启用 REST 端点。您可以通过添加参数 'show_in_rest' => true:

来做到这一点
<php
register_taxonomy( 'location_category', 'post', [
    // ...
    'show_in_rest' => true, // ← make sure you have this line in the taxonomy args!
] );

注意:如果您不使用 show_in_rest,则块编辑器(“Gutenberg”)中的分类不可用。


我强烈建议不要创建自定义 REST 端点来复制内置逻辑。
IE。不要做 or