【问题标题】:wordpress rest api v2 how to list taxonomy terms?wordpress rest api v2 如何列出分类术语?
【发布时间】:2017-07-16 16:12:42
【问题描述】:

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

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

但在 v2 中我尝试 /分类法/术语 它返回 json 错误 "code":"rest_no_route","message":"没有找到匹配 URL 和请求方法的路由","data":{"status" :404}}

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

我在谷歌上搜索了几个小时的问题没有显示任何结果,请任何人帮忙,谢谢

【问题讨论】:

标签: wordpress wordpress-rest-api


【解决方案1】:

在这里写自定义代码

将打击代码添加到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;
});

然后输入网址http://youdomain.com/wp-json/wp/v2/all-terms?term=you_taxonomy

所以 term = you_taxonomy,会得到属于 job_category 的术语。

【讨论】:

  • 这是天才。
  • 这救了我!!纯粹的天才。
  • 非常好!谢谢如果您想真正显示所有术语,您可以将一组参数添加到get_terms - 像这样:php get_terms($taxonomy_name, array( 'hide_empty' => false));
【解决方案2】:

分类术语是这样称呼的:

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

【讨论】:

  • 不过,这与检索特定分类中的所有术语无关。
  • 这是正确的答案。接受的答案是重新创建一个已经存在的函数。需要注意的一点是,如果您想要超过 10 个结果,则应在 URL 末尾添加 ?per_page=100。如果您有超过 100 个结果。您需要使用一些分页,即获取第二页:/wp-json/wp/v2/priority-tags?per_page=100&page=2
【解决方案3】:

对于自定义分类,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/)。

【讨论】:

    【解决方案4】:

    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。不要做thisthis

    【讨论】:

      【解决方案5】:

      接受的答案主要对我有用。这就是我得到的

      <?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/

      【讨论】:

        【解决方案6】:

        如果将来有人阅读此内容,我会遇到一个问题,即默认 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,
                    ));
                }
            }
        

        【讨论】:

          【解决方案7】:

          对于一些开发者来说似乎有些困惑,甚至对我来说也是如此。

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

          不是 https://example.com/wp-json/wp/v2/taxonomies/{your_taxonomy}

          “/分类”不需要

          【讨论】:

          • 这没有提供问题的答案。请改写评论。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-09-15
          • 2013-05-25
          • 1970-01-01
          相关资源
          最近更新 更多