通过 WordPress REST API 获å–文章特色图片 - 看è§å°ç»ƒ

迷失的人迷失了,相逢的人会å†ç›¸é€¢ã€‚

通过 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": "http://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://wavelpc.oss-cn-guangzhou.aliyuncs.com/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": "http://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://wavelpc.oss-cn-guangzhou.aliyuncs.com/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://wavelpc.oss-cn-guangzhou.aliyuncs.com/2016/11/angkor_wat_sunrise-150x150.jpg" }, "full": { "file": "angkor_wat_sunrise.jpg", "width": 750, "height": 300, "mime_type": "image/jpeg", "source_url": "https://wavelpc.oss-cn-guangzhou.aliyuncs.com/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://wavelpc.oss-cn-guangzhou.aliyuncs.com/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,ä¸ç„¶å°±å–ä¸åˆ°å€¼äº†ã€‚

6,320 次
分享

gravatar头åƒ