首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Wordpress REST响应仅显示具有ID的分类类别

Wordpress REST响应仅显示具有ID的分类类别
EN

Stack Overflow用户
提问于 2020-01-21 17:15:00
回答 1查看 414关注 0票数 0

我使用WP作为ACF en ACF-2-REST插件的无头CMS。我已经向post-type添加了类别,当我调用GET时,它会向我显示特定帖子的所有信息,包括类别,但只显示ID。如果我想匹配它,我必须再次调用类别来获取该类别的信息(名称、父级等)。

如何在post调用中显示该信息而不仅仅是ID?

JSON现在如何看待/activities调用:

代码语言:javascript
运行
复制
{
"id":111,
"date":"2020-01-18T15:39:27",
"date_gmt":"2020-01-18T15:39:27",
"guid":{"rendered":"https:\/\/url.be\/?post_type=activities&p=111"},
"modified":"2020-01-18T15:39:27",
"modified_gmt":"2020-01-18T15:39:27",
"slug":"walking-on-wood",
"status":"publish",
"type":"activities",
"link":"https:\/\/url.be\/activities\/walking-on-wood\/",
"title":{"rendered":"Walking on wood"},
"template":"",
"categories":[14,25,13,2,18,21,6,24],
"acf":{...}
}

我希望在“类别”中显示的内容,而不仅仅是数字(来自类别调用)

代码语言:javascript
运行
复制
{
"id":3,
"count":1,
"description":"",
"link":"https:\/\/url.be\/category\/duration\/lower-than-30-min\/",
"name":"< 30 min.",
"slug":"lower-than-30-min",
"taxonomy":"category",
"parent":2,"meta":[],
"acf":[],
"_links":{"self":[{"href":"https:\/\/url.be\/wp-json\/wp\/v2\/categories\/3"}],
"collection":[{"href":"https:\/\/url.be\/wp-json\/wp\/v2\/categories"}],
"about":[{"href":"https:\/\/url.be\/wp-json\/wp\/v2\/taxonomies\/category"}],
"up":[{"embeddable":true,"href":"https:\/\/url.be\/wp-json\/wp\/v2\/categories\/2"}],
"wp:post_type":[{"href":"https:\/\/url.be\/wp-json\/wp\/v2\/posts?categories=3"},{"href":"https:\/\/url.be\/wp-json\/wp\/v2\/activities?categories=3"}],
"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}
}

我在互联网上找不到任何解决方案,如果有人给我指出正确的方向,我会非常感激。谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-21 20:50:19

正如评论部分中所讨论的,这个问题的一个解决方案是使用WP REST API的自定义端点方法,并在其中执行额外的查询以获得所需的数据。这样,您就可以执行所有的数据操作来获得完美的响应,从而得到一个REST调用。

取自official developer docs

定义一个端点方法并添加一些额外的数据

代码语言:javascript
运行
复制
<?php
add_action( 'rest_api_init', function () {
  register_rest_route( 'myplugin/v1', '/author/(?P<id>\d+)', array(
    'methods' => 'GET',
    'callback' => 'my_awesome_func', //note that this is the method it will fire
    'args' => array(
      'id' => array(
        'validate_callback' => function($param, $request, $key) {
          return is_numeric( $param );
        }
      ),
    ),
  ) );


/**
 * Grab latest post by an author along with its category!
 *
 * @param array $data Options for the function.
 * @return array Post,
 */
function my_awesome_func( $data ) {

  $posts = get_posts( array(
    'author' => $data['id'],
  ) );

  if(!empty($posts)) {
    //Example of appending extra data
    foreach($posts as $post) {
       $category = wp_get_post_terms($post->ID, 'category');
       $post['category'] = $category; 
    }
    return $posts;

  } else  {
    return new WP_Error( 'no_author', 'Invalid author', array( 'status' => 404 ) );
  }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59837635

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档