通过 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,ä¸ç„¶å°±å–ä¸åˆ°å€¼äº†ã€‚