通过 WordPress REST API 获取文章特色图片

  WordPress 从 4.4 版本开始就提供了 WordPress REST API。通过 HTTP 的方式访问对应的链接,就可以以 JSON 格式获取到 WordPress 的内容(如文章、页面、分类等)。

  WordPress 提供的获取文章内容的链接,默认只返回文章的一些基础数据,那么如果通过 WordPress REST API 获取文章特色图片呢?

  以获取《吴哥记忆——柬埔寨暹粒四日行程(2016.11)》的内容为例,我们先来看看默认的链接:

https://www.wavelpc.com/wp-json/wp/v2/posts/3394

  访问这个链接返回以下数据:

{
    "id": 3394, 
    "date": "2016-11-27T23:20:23", 
    "date_gmt": "2016-11-27T15:20:23", 
    "guid": {
        "rendered": "https://www.wavelpc.com/?p=3394"
    }, 
    "modified": "2017-10-17T01:17:59", 
    "modified_gmt": "2017-10-16T17:17:59", 
    "slug": "%e6%88%91%e7%9a%84%e6%9a%b9%e7%b2%92%e5%90%b4%e5%93%a5%e5%9b%9b%e6%97%a5%e6%b8%b8%e8%a1%8c%e7%a8%8b", 
    "status": "publish", 
    "type": "post", 
    "link": "https://www.wavelpc.com/blog/3394.html", 
    "title": {
        "rendered": "吴哥记忆——柬埔寨暹粒四日行程(2016.11)"
    }, 
    "content": {
        "rendered": "

  小练前几天去了一趟暹粒,18号(周五)去21号(周一)回,并没有完整的三天来体验吴哥的大小外三圈,所以就根据自己的实际情况设计了行程。但是计划赶不上变化,例如第一天下午买票之后的巴肯山日落就因为大雨取消了,下边会附上行程的详情,供大家参考。

  出发前,我直接把行程打印下来了,给了司机一份,他们就按照这行程选择了最优的游览路线带我们游览,下边这一份是根据实际行程做了调整的。其中每个时间指的是从酒店出发的时间点,前边有 ** 的是推荐必游,前边带>的表示是大景点里边包含的小点,划了删除线的是因为实际情况取消了的行程。【】里边是大概的游览时间。

【以下内容省略】", "protected": false }, "excerpt": { "rendered": "

  小练前几天去了一趟暹粒,18号(周五)去21号(周一)回,并没有完整的三天来体验吴哥的大小外三圈 …

", "protected": false }, "author": 1, "featured_media": 3399, "comment_status": "open", "ping_status": "closed", "sticky": false, "template": "", "format": "standard", "meta": [ ], "categories": [ 225 ], "tags": [ 222, 227 ], "post_thumbnail_image": "https://oss.dellest.net/2016/11/angkor_wat_sunrise-150x150.jpg", "content_first_image": null, "post_medium_image_300": null, "post_thumbnail_image_624": null, "pageviews": 3, "total_comments": 0, "category_name": "那些风景", "like_count": "0", "avatarurls": [ ], "_links": { "self": [ { "href": "https://www.wavelpc.com/wp-json/wp/v2/posts/3394" } ], "collection": [ { "href": "https://www.wavelpc.com/wp-json/wp/v2/posts" } ], "about": [ { "href": "https://www.wavelpc.com/wp-json/wp/v2/types/post" } ], "author": [ { "embeddable": true, "href": "https://www.wavelpc.com/wp-json/wp/v2/users/1" } ], "replies": [ { "embeddable": true, "href": "https://www.wavelpc.com/wp-json/wp/v2/comments?post=3394" } ], "version-history": [ { "href": "https://www.wavelpc.com/wp-json/wp/v2/posts/3394/revisions" } ], "wp:featuredmedia": [ { "embeddable": true, "href": "https://www.wavelpc.com/wp-json/wp/v2/media/3399" } ], "wp:attachment": [ { "href": "https://www.wavelpc.com/wp-json/wp/v2/media?parent=3394" } ], "wp:term": [ { "taxonomy": "category", "embeddable": true, "href": "https://www.wavelpc.com/wp-json/wp/v2/categories?post=3394" }, { "taxonomy": "post_tag", "embeddable": true, "href": "https://www.wavelpc.com/wp-json/wp/v2/tags?post=3394" } ], "curies": [ { "name": "wp", "href": "https://api.w.org/{rel}", "templated": true } ] } }

  可以发现返回的数据包里边有一个 featured_media ,这个是特色图片对应的 ID,并不能直接用于展示。其实 WordPress 还为这个接口提供了一个参数,我们只要在原有链接后边加上 ?_embed=true ,就可以获取到特色图片的地址了,链接如下:

https://www.wavelpc.com/wp-json/wp/v2/posts/3394?_embed=true

  此时返回的数据如下:

{
    "id": 3394, 
    "date": "2016-11-27T23:20:23", 
    "date_gmt": "2016-11-27T15:20:23", 
    "guid": {
        "rendered": "https://www.wavelpc.com/?p=3394"
    }, 
    "modified": "2017-10-17T01:17:59", 
    "modified_gmt": "2017-10-16T17:17:59", 
    "slug": "%e6%88%91%e7%9a%84%e6%9a%b9%e7%b2%92%e5%90%b4%e5%93%a5%e5%9b%9b%e6%97%a5%e6%b8%b8%e8%a1%8c%e7%a8%8b", 
    "status": "publish", 
    "type": "post", 
    "link": "https://www.wavelpc.com/blog/3394.html", 
    "title": {
        "rendered": "吴哥记忆——柬埔寨暹粒四日行程(2016.11)"
    }, 
    "content": {
        "rendered": "

  小练前几天去了一趟暹粒,18号(周五)去21号(周一)回,并没有完整的三天来体验吴哥的大小外三圈,所以就根据自己的实际情况设计了行程。但是计划赶不上变化,例如第一天下午买票之后的巴肯山日落就因为大雨取消了,下边会附上行程的详情,供大家参考。

  出发前,我直接把行程打印下来了,给了司机一份,他们就按照这行程选择了最优的游览路线带我们游览,下边这一份是根据实际行程做了调整的。其中每个时间指的是从酒店出发的时间点,前边有 ** 的是推荐必游,前边带>的表示是大景点里边包含的小点,划了删除线的是因为实际情况取消了的行程。【】里边是大概的游览时间。

【以下内容省略】", "protected": false }, "excerpt": { "rendered": "

  小练前几天去了一趟暹粒,18号(周五)去21号(周一)回,并没有完整的三天来体验吴哥的大小外三圈 …

", "protected": false }, "author": 1, "featured_media": 3399, "comment_status": "open", "ping_status": "closed", "sticky": false, "template": "", "format": "standard", "meta": [ ], "categories": [ 225 ], "tags": [ 222, 227 ], "post_thumbnail_image": "https://oss.dellest.net/2016/11/angkor_wat_sunrise-150x150.jpg", "content_first_image": null, "post_medium_image_300": null, "post_thumbnail_image_624": null, "pageviews": 3, "total_comments": 0, "category_name": "那些风景", "like_count": "0", "avatarurls": [ ], "_links": { "self": [ { "href": "https://www.wavelpc.com/wp-json/wp/v2/posts/3394" } ], "collection": [ { "href": "https://www.wavelpc.com/wp-json/wp/v2/posts" } ], "about": [ { "href": "https://www.wavelpc.com/wp-json/wp/v2/types/post" } ], "author": [ { "embeddable": true, "href": "https://www.wavelpc.com/wp-json/wp/v2/users/1" } ], "replies": [ { "embeddable": true, "href": "https://www.wavelpc.com/wp-json/wp/v2/comments?post=3394" } ], "version-history": [ { "href": "https://www.wavelpc.com/wp-json/wp/v2/posts/3394/revisions" } ], "wp:featuredmedia": [ { "embeddable": true, "href": "https://www.wavelpc.com/wp-json/wp/v2/media/3399" } ], "wp:attachment": [ { "href": "https://www.wavelpc.com/wp-json/wp/v2/media?parent=3394" } ], "wp:term": [ { "taxonomy": "category", "embeddable": true, "href": "https://www.wavelpc.com/wp-json/wp/v2/categories?post=3394" }, { "taxonomy": "post_tag", "embeddable": true, "href": "https://www.wavelpc.com/wp-json/wp/v2/tags?post=3394" } ], "curies": [ { "name": "wp", "href": "https://api.w.org/{rel}", "templated": true } ] }, "_embedded": { "author": [ { "code": "rest_no_route", "message": "未找到匹配URL和请求方式的路由。", "data": { "status": 404 } } ], "wp:featuredmedia": [ { "id": 3399, "date": "2016-11-27T23:28:23", "slug": "angkor_wat_sunrise", "type": "attachment", "link": "https://www.wavelpc.com/blog/3394.html/angkor_wat_sunrise", "title": { "rendered": "angkor_wat_sunrise" }, "author": 1, "caption": { "rendered": "" }, "alt_text": "", "media_type": "image", "mime_type": "image/jpeg", "media_details": { "width": 750, "height": 300, "file": "2016/11/angkor_wat_sunrise.jpg", "sizes": { "thumbnail": { "file": "angkor_wat_sunrise-150x150.jpg", "width": 150, "height": 150, "mime_type": "image/jpeg", "source_url": "https://oss.dellest.net/2016/11/angkor_wat_sunrise-150x150.jpg" }, "full": { "file": "angkor_wat_sunrise.jpg", "width": 750, "height": 300, "mime_type": "image/jpeg", "source_url": "https://oss.dellest.net/2016/11/angkor_wat_sunrise.jpg" } }, "image_meta": { "aperture": "2.2", "credit": "", "camera": "PRO 5", "caption": "", "created_timestamp": "1479536247", "copyright": "", "focal_length": "4.73", "iso": "40", "shutter_speed": "0.00460829493088", "title": "", "orientation": "1", "keywords": [ ] } }, "source_url": "https://oss.dellest.net/2016/11/angkor_wat_sunrise.jpg", "_links": { "self": [ { "href": "https://www.wavelpc.com/wp-json/wp/v2/media/3399" } ], "collection": [ { "href": "https://www.wavelpc.com/wp-json/wp/v2/media" } ], "about": [ { "href": "https://www.wavelpc.com/wp-json/wp/v2/types/attachment" } ], "author": [ { "embeddable": true, "href": "https://www.wavelpc.com/wp-json/wp/v2/users/1" } ], "replies": [ { "embeddable": true, "href": "https://www.wavelpc.com/wp-json/wp/v2/comments?post=3399" } ] } } ], "wp:term": [ [ { "id": 225, "link": "https://www.wavelpc.com/blog/category/scenery", "name": "那些风景", "slug": "scenery", "taxonomy": "category", "category_thumbnail_image": "", "_links": { "self": [ { "href": "https://www.wavelpc.com/wp-json/wp/v2/categories/225" } ], "collection": [ { "href": "https://www.wavelpc.com/wp-json/wp/v2/categories" } ], "about": [ { "href": "https://www.wavelpc.com/wp-json/wp/v2/taxonomies/category" } ], "wp:post_type": [ { "href": "https://www.wavelpc.com/wp-json/wp/v2/posts?categories=225" } ], "curies": [ { "name": "wp", "href": "https://api.w.org/{rel}", "templated": true } ] } } ], [ { "id": 222, "link": "https://www.wavelpc.com/blog/tag/%e6%97%85%e8%a1%8c", "name": "旅行", "slug": "%e6%97%85%e8%a1%8c", "taxonomy": "post_tag", "_links": { "self": [ { "href": "https://www.wavelpc.com/wp-json/wp/v2/tags/222" } ], "collection": [ { "href": "https://www.wavelpc.com/wp-json/wp/v2/tags" } ], "about": [ { "href": "https://www.wavelpc.com/wp-json/wp/v2/taxonomies/post_tag" } ], "wp:post_type": [ { "href": "https://www.wavelpc.com/wp-json/wp/v2/posts?tags=222" } ], "curies": [ { "name": "wp", "href": "https://api.w.org/{rel}", "templated": true } ] } }, { "id": 227, "link": "https://www.wavelpc.com/blog/tag/%e8%a1%8c%e7%a8%8b", "name": "行程", "slug": "%e8%a1%8c%e7%a8%8b", "taxonomy": "post_tag", "_links": { "self": [ { "href": "https://www.wavelpc.com/wp-json/wp/v2/tags/227" } ], "collection": [ { "href": "https://www.wavelpc.com/wp-json/wp/v2/tags" } ], "about": [ { "href": "https://www.wavelpc.com/wp-json/wp/v2/taxonomies/post_tag" } ], "wp:post_type": [ { "href": "https://www.wavelpc.com/wp-json/wp/v2/posts?tags=227" } ], "curies": [ { "name": "wp", "href": "https://api.w.org/{rel}", "templated": true } ] } } ] ] } }

  可以注意到 source_url 就是特色图片的地址。其实这个链接返回了大量可以直接用于显示的数据,这个大家可以各取所需。

  需要注意的是通过数组取 source_url 的方式是:

$array['_embedded']['wp:featuredmedia'][0]['source_url'] 

  可千万不能忘了 wp:featuredmedia 接下来的下标是 0,不然就取不到值了。