简体   繁体   English

WP API 获取帖子标签

[英]WP API Get Post Tags

Trying to get post tags with wordpress API - api call is /wp-json/wp/v2/posts尝试使用 wordpress API 获取帖子标签 - api 调用是 /wp-json/wp/v2/posts

ourHTMLString += '<i class="fa fa-tags">"' + postsData[i].tags + '"</i>';

It is returning these values它正在返回这些值

"tags": [
        766,
        19,
        578
],

I need the tag name and href to this, not sure how to get this.我需要标签名称和href,不知道如何获得。 I have tried postsData[i].wp:term[i].tag.name - cannot find a solution.我已经尝试过postsData[i].wp:term[i].tag.name - 找不到解决方案。 Any help?有什么帮助吗? thanks谢谢

I think you need to do another request to get this and use include to list only theses tags.我认为你需要做另一个请求来获得这个并使用include只列出这些标签。 eg: /wp-json/wp/v2/tags?include=766,19,578例如:/wp-json/wp/v2/tags?include=766,19,578

https://developer.wordpress.org/rest-api/reference/tags/ https://developer.wordpress.org/rest-api/reference/tags/

Send a request to the Wordpress site with the tag's id:使用标签的 id 向 Wordpress 站点发送请求:

http://demo.wp-api.org/wp-json/wp/v2/tags/TagID

REF : https://developer.wordpress.org/rest-api/reference/tags/#definition参考: https : //developer.wordpress.org/rest-api/reference/tags/#definition

Definition定义

GET /wp/v2/tags/获取/wp/v2/标签/

Example Request示例请求

$ curl http://demo.wp-api.org/wp-json/wp/v2/tags/ $ curl http://demo.wp-api.org/wp-json/wp/v2/tags/

If we need tags in same API call we can add a custom field in our response.如果我们需要在同一个 API 调用中使用标签,我们可以在响应中添加一个自定义字段。

we can add following code in theme's function.php file我们可以在主题的function.php文件中添加以下代码

add_action('rest_api_init', 'bs_rest_api_hooks');
function bs_rest_api_hooks() {
    register_rest_field(
        'post',
        'mtags',
        array(
            'get_callback' => 'm_get_tags',
        )
    );
}
function m_get_tags($object, $field_name, $request) {

    $tags = get_the_tags($object["id"]);

    return $tags;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM