最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - WP API Get Post Tags - Stack Overflow

programmeradmin0浏览0评论

Trying to get post tags with wordpress API - api call is /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. I have tried postsData[i].wp:term[i].tag.name - cannot find a solution. Any help? thanks

Trying to get post tags with wordpress API - api call is /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. I have tried postsData[i].wp:term[i].tag.name - cannot find a solution. Any help? thanks

Share Improve this question asked Jun 21, 2017 at 3:06 roshamboroshambo 2,8048 gold badges32 silver badges57 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

I think you need to do another request to get this and use include to list only theses tags. eg: /wp-json/wp/v2/tags?include=766,19,578

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

Send a request to the Wordpress site with the tag's id:

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

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

Definition

GET /wp/v2/tags/

Example Request

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

If we need tags in same API call we can add a custom field in our response.

we can add following code in theme's function.php file

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;
}
发布评论

评论列表(0)

  1. 暂无评论