3

我在博客上安装了 WordPress REST API 插件,我想用它把博客文章中的内容拉到另一个网站的面板窗口中。文本内容很好,但我不确定是否可以获得“精选图片”网址。我想要 URL,以便可以将其存储在变量中,然后将其用作面板的背景图像 CSS(在 NON-WORDPRESS 站点的面板窗口中显示博客数据)。

以下是返回的数据馈送示例(只是第一个条目):

[{
    "id": 15470,
    "date": "2015-05-21T15:56:31",
    "date_gmt": "2015-05-21T05:56:31",
    "guid": {
        "rendered": "http:\/\/blog2.slq.qld.gov.au\/jol\/?p=15470"
    },
    "modified": "2016-06-06T10:01:53",
    "modified_gmt": "2016-06-06T00:01:53",
    "slug": "test-post-3",
    "type": "post",
    "link": "http:\/\/blog2.slq.qld.gov.au\/jol\/2015\/05\/21\/test-post-3\/",
    "title": {
        "rendered": "test post"
    },
    "content": {
        "rendered": "<p>[slideshow_deploy id=&#8217;15471&#8242;]<\/p>\n<p>WP REST API plugins is great for getting your blog content out into another website.<\/p>\n<div id=\"attachment_15405\" style=\"width: 160px\" class=\"wp-caption alignleft\"><a href=\"http:\/\/blog2.slq.qld.gov.au\/jol\/files\/2014\/08\/Hotel-Metropole-Thursday-Island-1897.jpg\" rel=\"attachment wp-att-15405\"><img class=\"size-thumbnail wp-image-15405\" src=\"http:\/\/blog2.slq.qld.gov.au\/jol\/files\/2014\/08\/Hotel-Metropole-Thursday-Island-1897-150x150.jpg\" alt=\"Hotel Metropole, Thursday Island, 1897\" width=\"150\" height=\"150\" \/><\/a><p class=\"wp-caption-text\">Hotel Metropole, Thursday Island, 1897<\/p><\/div>\n"
    },
    "excerpt": {
        "rendered": "<p>[slideshow_deploy id=&#8217;15471&#8242;] WP REST API plugins is great for getting your blog content out into another website.<\/p>\n"
    },
    "author": 11,
    "featured_media": 15392,
    "comment_status": "open",
    "ping_status": "open",
    "sticky": false,
    "format": "standard",
    "categories": [1],
    "tags": [],
    "_links": {
        "self": [{
            "href": "http:\/\/blog2.slq.qld.gov.au\/jol\/wp-json\/wp\/v2\/posts\/15470"
        }],
        "collection": [{
            "href": "http:\/\/blog2.slq.qld.gov.au\/jol\/wp-json\/wp\/v2\/posts"
        }],
        "about": [{
            "href": "http:\/\/blog2.slq.qld.gov.au\/jol\/wp-json\/wp\/v2\/types\/post"
        }],
        "author": [{
            "embeddable": true,
            "href": "http:\/\/blog2.slq.qld.gov.au\/jol\/wp-json\/wp\/v2\/users\/11"
        }],
        "replies": [{
            "embeddable": true,
            "href": "http:\/\/blog2.slq.qld.gov.au\/jol\/wp-json\/wp\/v2\/comments?post=15470"
        }],
        "version-history": [{
            "href": "http:\/\/blog2.slq.qld.gov.au\/jol\/wp-json\/wp\/v2\/posts\/15470\/revisions"
        }],
        "https:\/\/api.w.org\/featuredmedia": [{
            "embeddable": true,
            "href": "http:\/\/blog2.slq.qld.gov.au\/jol\/wp-json\/wp\/v2\/media\/15392"
        }],
        "https:\/\/api.w.org\/attachment": [{
            "href": "http:\/\/blog2.slq.qld.gov.au\/jol\/wp-json\/wp\/v2\/media?parent=15470"
        }],
        "https:\/\/api.w.org\/term": [{
            "taxonomy": "category",
            "embeddable": true,
            "href": "http:\/\/blog2.slq.qld.gov.au\/jol\/wp-json\/wp\/v2\/categories?post=15470"
        }, {
            "taxonomy": "post_tag",
            "embeddable": true,
            "href": "http:\/\/blog2.slq.qld.gov.au\/jol\/wp-json\/wp\/v2\/tags?post=15470"
        }]
    }
}]

现在我可以看到特色图片的条目是:

"featured_media": 15392,

然后在“_links”对象下方的下方有以下几行:

"https:\/\/api.w.org\/featuredmedia": [{
    "embeddable": true,
    "href": "http:\/\/blog2.slq.qld.gov.au\/jol\/wp-json\/wp\/v2\/media\/15392"
}],

当我尝试使用该 URL 时,它显然只是拉回了另一个 JSON 提要,然后它似乎将实际的特色图像 URL 拉回为:

"guid": {
    "rendered": "http:\/\/blog2.slq.qld.gov.au\/jol\/files\/2014\/08\/NT-app2.png"
},

当该路径可以转换为正确的 URL 时,我可以将其存储为变量并使用它。这里发生了什么?这是否像另一个 JSON 提要中的 JSON 提要?

如何提取特色图片 URL?我熟悉从 JSON 提要构建 HTML 内容,其中所有内容都可以从父提要中获得,但我不熟悉这种模式。

4

0 回答 0