Custom Taxonomy in REST API

时间:2021-04-14 作者:user3542212

我有两个自定义分类法,用于我的帖子。

我正试图从其中一个中检索名称。

在REST API V2中,我执行以下操作:

https://example.com/wp-json/wp/v2/posts/14340/?_embed=wp:term
这嵌入了所有分类法的实际名称(类别加上我的两个自定义分类法),但我只对与每个帖子相关联的名称感兴趣,只对其中一个自定义分类法感兴趣。

响应示例:

"_embedded": {
    "wp:term": [
        [
            {
                "id": 2272,
                "link": "https://example.com/category/cat1/",
                "name": "Cat1",
                "slug": "cat1",
                "taxonomy": "category",
                "_links": {
                ....
                }
            }
        ],
        [],
        [
            {
                "id": 1740,
                "link": "https://example.com/custom1/custom-name/",
                "name": "Custom Name",
                "slug": "custom-name",
                "taxonomy": "custom-tax-1",
                "_links": {
                ....
                }
            }
        ],
        [
            {
                "id": 2148,
                "link": "https://example.com/custom2/custom-name-1/",
                "name": "Custom Name 1",
                "slug": "custom-name-1",
                "taxonomy": "custom-tax-2",
                "_links": {
                ....
                }
            },
            {
                "id": 2150,
                "link": "https://example.com/custom2/custom-name-2/",
                "name": "Custom Name 2",
                "slug": "custom-name-2",
                "taxonomy": "custom-tax-2",
                "_links": {
                ....
                }
            }
        ]
    ]
}
所以我得到了这些分类法:

类别(1项)

  • 海关税-1(1项)
  • 海关税-2(2项)
    1. How can I make it so it only returns values for custom-tax-2?

    1 个回复
    SO网友:user3542212

    找到了答案。只需进行如下调用即可获取特定帖子的自定义分类法的值:

    https://example.com/wp-json/wp/v2/custom-tax-2?post={id}