【问题标题】:Display other WP site blog posts in one WP blog在一个 WP 博客中显示其他 WP 站点博客文章
【发布时间】:2016-08-17 16:44:56
【问题描述】:

一直在互联网上搜索有关如何执行此操作的想法。这是我想做的:

WP 站点“A”用作我运行的其他 WP 站点的各种门户,它也将拥有自己的博客文章。

WP 站点“B”是两个具有活跃博客的外部站点中的第一个。帖子有缩略图和标题。

WP 站点“C”是两个具有活动博客的外部站点中的第二个。帖子有缩略图和标题。

WP 站点“A”将在主页上有一个砖石风格的帖子列表、缩略图和链接的帖子标题。我想从站点“B”和“C”中提取新的博客文章,并以相同的方式显示它们:缩略图、文章标题和指向外部站点的链接。我还希望它按日期升序显示它们,并在创建新博客时穿插在一起。

是否有人对如何执行此操作或如何将外部帖子提供给另一个博客提出建议?如果有帮助,所有这些博客都在同一个托管帐户上,对它们的数据库具有相同的访问权限。我一直在寻找一些基于 RSS 的解决方案(实际上之前已经使用其中一个将单个帖子拉入主页,但这比这更复杂),但我不确定如何将它们集成到结构中的博客“A”,以便它们全部组合在同一个砌体网格中。

感谢您的帮助。

【问题讨论】:

    标签: php wordpress rss


    【解决方案1】:

    您可以查看 WP REST API (http://v2.wp-api.org)。这正是您正在寻找的。使用 WP REST API,您可以从您管理的任何站点获取帖子的 EVERY 元素,并将其显示在任何其他站点上。 (但你也应该实现某种形式的身份验证!)

    示例请求:

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

    “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
    

    如您所见,您可以从其他站点获取所需的每条信息 - 您甚至可以根据自己的喜好自定义响应。

    【讨论】:

    • 感谢您的快速回复。在看到另一个相关问题后,我实际上只是在查看 Rest API。在 1-10 的范围内,您将实际更新代码以集成我所描述的事物的复杂性排名在哪里?我精通PHP,但这看起来可能很复杂。有什么想法吗?
    • 从 1 到 10?我认为评价它比做它更难:) REST API 是一个插件(实际上它计划成为 WP 4.5 中的 WP 核心功能),所以安装它是小菜一碟。然后,API 页面(和其他教程)提供了很多关于检索您想要的数据的示例代码 - 我认为,当您可以在屏幕上看到响应时,它就变成了在您想要的地方放置和格式化数组元素的游戏.我建议使用一些测试软件(我使用 Chrome 的 Postman 扩展)来查看结果。身份验证是另一回事 - 这可能很困难,具体取决于您选择的类型。
    • 另外,还有一个问题 - 您是否认为可以使用 REST API 来完成我所描述的操作并将其他站点的帖子链接到这些特定站点?例如:在博客 A 上,单击指向博客 B 帖子的链接,博客 B 会在该帖子详细信息页面上打开?
    • 再次感谢,我真的很感激。如果我有任何问题,会通知您。
    • 不客气! :) 我发现 WP REST API 符合他们的承诺:WP 朝着成为一个成熟的 CMS 迈出了一大步。
    猜你喜欢
    • 2011-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-18
    • 1970-01-01
    • 2020-02-25
    • 1970-01-01
    相关资源
    最近更新 更多