2016-08-17 106 views
0

已經在網上搜尋關於如何做到這一點的想法。這裏是我想要做的:在一個WP博客中顯示其他WP網站博客文章

WP網站「A」被用作我運行的其他WP網站的排序門戶,它也將擁有自己的博客文章。

WP網站「B」是具有活動博客的兩個外部網站中的第一個。帖子有縮略圖和標題。

WP網站「C」是具有活動博客的兩個外部網站中的第二個。帖子有縮略圖和標題。

WP網站「A」將在首頁,縮略圖和鏈接的帖子標題上顯示磚石風格的帖子。我想從網站「B」和「C」中提取新的博客文章,並以相同的方式顯示它們:縮略圖,文章標題和通向外部網站的鏈接。我也希望這個以新的日期順序顯示出來,並在創建新博客時穿插在一起。

有沒有人有關於如何做到這一點,或如何將外部帖子送入另一個博客的建議?如果有幫助,所有這些博客都在同一個主機帳戶上,對其數據庫具有相同的訪問權限。我一直在找一些基於RSS的解決方案(事實上,他們之前曾使用其中的一個將單個帖子放入主頁,但這比涉及更多),但我不確定如何將它們集成到結構中的博客「A」,以便他們都在同一個石工網格中分組在一起。

感謝您的幫助。

回答

2

你可以看看WP REST API(http://v2.wp-api.org)。這正是你正在尋找的。使用WP REST API,您可以從您管理的任何站點獲得每個帖子的元素,並將其顯示在任何其他站點上。 (但你也應該實現某種形式的認證!)

請求示例:

http://www.example.com/wp/wp-json/wp/v2/venue 

「場地」是一個自定義類型後,我在真正的代碼中使用。示例請求是爲了從站點獲取所有場地。

迴應示例:

[ 
    { 
    "id": 20, 
    "date": "2016-06-08T16:37:23", 
    "date_gmt": "2016-06-08T16:37:23", 
    "guid": { 
     "rendered": "http://www.example.com/wp/?post_type=venue&p=20" 
    }, 
    "modified": "2016-06-20T11:45:22", 
    "modified_gmt": "2016-06-20T11:45:22", 
    "slug": "aquarium-club", 
    "type": "venue", 
    "link": "http://www.example.com/wp/venue/aquarium-club/", 
    "title": { 
     "rendered": "Aquarium Club" 
    }, 
    "content": { 
     "rendered": "<p>Aquarium Club. That&#8217;s OK. 7765e546uzfkjglh</p>\n<p>&nbsp;</p>\n<style>\n.gmap-iframe-container {\nmax-width: 100%;\n}\n.gmap-iframe {\nwidth: 100%;\nheight: 200px;\nmin-width:100%;\n}\n</style>\n<div class=\"gmap-iframe-container\">\n<iframe src=\"https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d2695.565888030695!2d19.052103115616678!3d47.49836967917771!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x4741dc402a04eee3%3A0x6869564cd433693c!2sAkv%C3%A1rium+Klub!5e0!3m2!1shu!2shu!4v1466147278469\" frameborder=\"0\" style=\"border:0\" allowfullscreen class=\"gmap-iframe\"></iframe></p>\n" 
    }, 
    "excerpt": { 
     "rendered": "<p>Aquarium Club. That&#8217;s OK. 7765e546uzfkjglh &nbsp;</p>\n" 
    }, 
    "featured_media": 0, 
    "menu_order": 0, 
    "format": "standard", 
    "tags": [], 
    "meta_data": { 
     "youtube_embed": "https://www.youtube.com/embed/xBW7DglTDGs" 
    }, 
    "_links": { 
     "self": [ 
     { 
      "href": "http://www.example.com/wp/wp-json/wp/v2/venue/20" 
     } 
     ], 
     "collection": [ 
     { 
      "href": "http://www.example.com/wp/wp-json/wp/v2/venue" 
     } 
     ], 
     "about": [ 
     { 
      "href": "http://www.example.com/wp/wp-json/wp/v2/types/venue" 
     } 
     ], 
     "wp:attachment": [ 
     { 
      "href": "http://www.example.com/wp/wp-json/wp/v2/media?parent=20" 
     } 
     ], 
     "wp:term": [ 
     { 
      "taxonomy": "post_tag", 
      "embeddable": true, 
      "href": "http://www.example.com/wp/wp-json/wp/v2/tags?post=20" 
     } 
     ], 
     "curies": [ 
     { 
      "name": "wp", 
      "href": "https://api.w.org/{rel}", 
      "templated": true 
     } 
     ] 
    } 
    }, 
    { 
    "id": 18, 
    "date": "2016-06-08T15:55:04", 
    "date_gmt": "2016-06-08T15:55:04", 
    "guid": { 
     "rendered": "http://www.example.com/wp/?post_type=venue&#038;p=18" 
    }, 
    "modified": "2016-06-08T15:55:04", 
    "modified_gmt": "2016-06-08T15:55:04", 
    "slug": "durer-kert", 
    "type": "venue", 
    "link": "http://www.example.com/wp/venue/durer-kert/", 
    "title": { 
     "rendered": "Dürer Kert" 
    }, 
    "content": { 
     "rendered": "" 
    }, 
    "excerpt": { 
     "rendered": "" 
    }, 
    "featured_media": 19, 
    "menu_order": 0, 
    "format": "standard", 
    "tags": [], 
    "meta_data": { 
     "youtube_embed": "https://www.youtube.com/embed/w9saGGpnKlk" 
    }, 
    "_links": { 
     "self": [ 
     { 
      "href": "http://www.example.com/wp/wp-json/wp/v2/venue/18" 
     } 
     ], 
     "collection": [ 
     { 
      "href": "http://www.example.com/wp/wp-json/wp/v2/venue" 
     } 
     ], 
     "about": [ 
     { 
      "href": "http://www.example.com/wp/wp-json/wp/v2/types/venue" 
     } 
     ], 
     "wp:featuredmedia": [ 
     { 
      "embeddable": true, 
      "href": "http://www.example.com/wp/wp-json/wp/v2/media/19" 
     } 
     ], 
     "wp:attachment": [ 
     { 
      "href": "http://www.example.com/wp/wp-json/wp/v2/media?parent=18" 
     } 
     ], 
     "wp:term": [ 
     { 
      "taxonomy": "post_tag", 
      "embeddable": true, 
      "href": "http://www.example.com/wp/wp-json/wp/v2/tags?post=18" 
     } 
     ], 
     "curies": [ 
     { 
      "name": "wp", 
      "href": "https://api.w.org/{rel}", 
      "templated": true 
     } 
     ] 
    } 
    }, 
    { 
    "id": 15, 
    "date": "2016-06-08T14:52:40", 
    "date_gmt": "2016-06-08T14:52:40", 
    "guid": { 
     "rendered": "http://www.example.com/wp/?post_type=venue&#038;p=15" 
    }, 
    "modified": "2016-06-08T15:41:27", 
    "modified_gmt": "2016-06-08T15:41:27", 
    "slug": "kobuci-kert", 
    "type": "venue", 
    "link": "http://www.example.com/wp/venue/kobuci-kert/", 
    "title": { 
     "rendered": "KOBUCI Kert" 
    }, 
    "content": { 
     "rendered": "<p>This is KOBUCI Kert.</p>\n" 
    }, 
    "excerpt": { 
     "rendered": "<p>This is KOBUCI Kert.</p>\n" 
    }, 
    "featured_media": 17, 
    "menu_order": 0, 
    "format": "standard", 
    "tags": [], 
    "meta_data": { 
     "youtube_embed": "https://www.youtube.com/embed/zwNBKh6ghOY" 
    }, 
    "_links": { 
     "self": [ 
     { 
      "href": "http://www.example.com/wp/wp-json/wp/v2/venue/15" 
     } 
     ], 
     "collection": [ 
     { 
      "href": "http://www.example.com/wp/wp-json/wp/v2/venue" 
     } 
     ], 
     "about": [ 
     { 
      "href": "http://www.example.com/wp/wp-json/wp/v2/types/venue" 
     } 
     ], 
     "wp:featuredmedia": [ 
     { 
      "embeddable": true, 
      "href": "http://www.example.com/wp/wp-json/wp/v2/media/17" 
     } 
     ], 
     "wp:attachment": [ 
     { 
      "href": "http://www.example.com/wp/wp-json/wp/v2/media?parent=15" 
     } 
     ], 
     "wp:term": [ 
     { 
      "taxonomy": "post_tag", 
      "embeddable": true, 
      "href": "http://www.example.com/wp/wp-json/wp/v2/tags?post=15" 
     } 
     ], 
     "curies": [ 
     { 
      "name": "wp", 
      "href": "https://api.w.org/{rel}", 
      "templated": true 
     } 
     ] 
    } 
    }, 
    { 
    "id": 13, 
    "date": "2016-06-08T14:12:38", 
    "date_gmt": "2016-06-08T14:12:38", 
    "guid": { 
     "rendered": "http://www.example.com/wp/?post_type=venue&#038;p=13" 
    }, 
    "modified": "2016-06-08T14:12:38", 
    "modified_gmt": "2016-06-08T14:12:38", 
    "slug": "mupa", 
    "type": "venue", 
    "link": "http://www.example.com/wp/venue/mupa/", 
    "title": { 
     "rendered": "MÜPA" 
    }, 
    "content": { 
     "rendered": "<p>MÜPA is MÜPA. Not bad from a building, is it? A lot of concerts and stuff.</p>\n" 
    }, 
    "excerpt": { 
     "rendered": "<p>MÜPA is MÜPA. Not bad from a building, is it? A lot of concerts and stuff.</p>\n" 
    }, 
    "featured_media": 14, 
    "menu_order": 0, 
    "format": "standard", 
    "tags": [], 
    "meta_data": { 
     "youtube_embed": "https://www.youtube.com/embed/YnOW2rUXuSg" 
    }, 
    "_links": { 
     "self": [ 
     { 
      "href": "http://www.example.com/wp/wp-json/wp/v2/venue/13" 
     } 
     ], 
     "collection": [ 
     { 
      "href": "http://www.example.com/wp/wp-json/wp/v2/venue" 
     } 
     ], 
     "about": [ 
     { 
      "href": "http://www.example.com/wp/wp-json/wp/v2/types/venue" 
     } 
     ], 
     "wp:featuredmedia": [ 
     { 
      "embeddable": true, 
      "href": "http://www.example.com/wp/wp-json/wp/v2/media/14" 
     } 
     ], 
     "wp:attachment": [ 
     { 
      "href": "http://www.example.com/wp/wp-json/wp/v2/media?parent=13" 
     } 
     ], 
     "wp:term": [ 
     { 
      "taxonomy": "post_tag", 
      "embeddable": true, 
      "href": "http://www.example.com/wp/wp-json/wp/v2/tags?post=13" 
     } 
     ], 
     "curies": [ 
     { 
      "name": "wp", 
      "href": "https://api.w.org/{rel}", 
      "templated": true 
     } 
     ] 
    } 
    }, 
    { 
    "id": 8, 
    "date": "2016-06-07T08:36:40", 
    "date_gmt": "2016-06-07T08:36:40", 
    "guid": { 
     "rendered": "http://www.example.com/wp/?post_type=venue&#038;p=8" 
    }, 
    "modified": "2016-06-08T13:22:39", 
    "modified_gmt": "2016-06-08T13:22:39", 
    "slug": "some-venue", 
    "type": "venue", 
    "link": "http://www.example.com/wp/venue/some-venue/", 
    "title": { 
     "rendered": "National Concert Hall" 
    }, 
    "content": { 
     "rendered": "<p>This is the next venue for REST API. Just to see more than one. This should be the general description of this venue. This is the venue &#8211; what is it? 5324</p>\n" 
    }, 
    "excerpt": { 
     "rendered": "<p>This is the next venue for REST API. Just to see more than one. This should be the general description of this venue. This is the venue &#8211; what is it? 5324</p>\n" 
    }, 
    "featured_media": 9, 
    "menu_order": 0, 
    "format": "standard", 
    "tags": [], 
    "meta_data": { 
     "youtube_embed": "https://www.youtube.com/embed/camVVzjGheQ" 
    }, 
    "_links": { 
     "self": [ 
     { 
      "href": "http://www.example.com/wp/wp-json/wp/v2/venue/8" 
     } 
     ], 
     "collection": [ 
     { 
      "href": "http://www.example.com/wp/wp-json/wp/v2/venue" 
     } 
     ], 
     "about": [ 
     { 
      "href": "http://www.example.com/wp/wp-json/wp/v2/types/venue" 
     } 
     ], 
     "wp:featuredmedia": [ 
     { 
      "embeddable": true, 
      "href": "http://www.example.com/wp/wp-json/wp/v2/media/9" 
     } 
     ], 
     "wp:attachment": [ 
     { 
      "href": "http://www.example.com/wp/wp-json/wp/v2/media?parent=8" 
     } 
     ], 
     "wp:term": [ 
     { 
      "taxonomy": "post_tag", 
      "embeddable": true, 
      "href": "http://www.example.com/wp/wp-json/wp/v2/tags?post=8" 
     } 
     ], 
     "curies": [ 
     { 
      "name": "wp", 
      "href": "https://api.w.org/{rel}", 
      "templated": true 
     } 
     ] 
    } 
    }, 
    { 
    "id": 7, 
    "date": "2016-06-07T08:33:13", 
    "date_gmt": "2016-06-07T08:33:13", 
    "guid": { 
     "rendered": "http://www.example.com/wp/?post_type=venue&#038;p=7" 
    }, 
    "modified": "2016-06-08T13:20:27", 
    "modified_gmt": "2016-06-08T13:20:27", 
    "slug": "orchestra-hall", 
    "type": "venue", 
    "link": "http://www.example.com/wp/venue/orchestra-hall/", 
    "title": { 
     "rendered": "Music Academy" 
    }, 
    "content": { 
     "rendered": "<p>This is a venue &#8211; it is made of a custom post type, and I get it with REST API.</p>\n" 
    }, 
    "excerpt": { 
     "rendered": "<p>This is a venue &#8211; it is made of a custom post type, and I get it with REST API.</p>\n" 
    }, 
    "featured_media": 11, 
    "menu_order": 0, 
    "format": "standard", 
    "tags": [], 
    "meta_data": { 
     "youtube_embed": "https://www.youtube.com/embed/ZL6BFCxNGZ8" 
    }, 
    "_links": { 
     "self": [ 
     { 
      "href": "http://www.example.com/wp/wp-json/wp/v2/venue/7" 
     } 
     ], 
     "collection": [ 
     { 
      "href": "http://www.example.com/wp/wp-json/wp/v2/venue" 
     } 
     ], 
     "about": [ 
     { 
      "href": "http://www.example.com/wp/wp-json/wp/v2/types/venue" 
     } 
     ], 
     "wp:featuredmedia": [ 
     { 
      "embeddable": true, 
      "href": "http://www.example.com/wp/wp-json/wp/v2/media/11" 
     } 
     ], 
     "wp:attachment": [ 
     { 
      "href": "http://www.example.com/wp/wp-json/wp/v2/media?parent=7" 
     } 
     ], 
     "wp:term": [ 
     { 
      "taxonomy": "post_tag", 
      "embeddable": true, 
      "href": "http://www.example.com/wp/wp-json/wp/v2/tags?post=7" 
     } 
     ], 
     "curies": [ 
     { 
      "name": "wp", 
      "href": "https://api.w.org/{rel}", 
      "templated": true 
     } 
     ] 
    } 
    } 
] 

注: - 上面的例子的結果是有點定製,以我的需求 - 所以你可能會看到有些其他的結果。 - 單個地點的請求會是這個樣子:

http://www.example.com/wp/wp-json/wp/v2/venue/20 

正如你看到的,你可以得到的每一條信息,你從其他網站需要 - 你甚至可以定製自己的喜好響應。

+0

感謝您的快速回復。在我看到另一個相關問題之後,我實際上只是在查看Rest API。按照1-10的規模,您如何評估實際更新代碼的複雜性,以便如我所描述的那樣進行集成?我精通PHP,但這看起來可能非常複雜。有什麼想法嗎? – mg33

+0

從1-10開始?我認爲這比它更難評價:) REST API是一個插件(實際上它計劃是WP 4.5中的WP核心功能),所以安裝它是小菜一碟。然後,API頁面(和其他教程)提供了很多示例代碼來獲取您想要的數據 - 我認爲,當您的屏幕上可以看到響應時,它會成爲您要放置和格式化數組元素的遊戲。我建議使用一些測試軟件(我使用Chrome的Postman擴展)來查看結果。認證是另一回事 - 根據您選擇的類型,認證可能很難。 –

+0

此外,還有一個問題 - 您是否認爲可以使用REST API來完成我所描述的內容,並將其他站點的帖子鏈接到這些特定站點?例如:在博客A上,點擊博客B博文的鏈接,博客B在該博文的詳細信息頁面上打開? – mg33