将内容从一个 wordpress 站点拉到另一个 wordpress 站点

Pull content from one wordpress site to another wordpress site

我正在尝试找到一种在不同站点上显示来自一个网站的文本的方法。

我拥有这两个网站,并且它们都 运行 在 wordpress 上(我知道这可能会使它变得更加困难)。我只需要一个页面来镜像页面中的文本,当原始页面更新时,镜像也会更新。

我对PHP和HTML有一些经验,我也不愿意用Js。 我一直在查看一些建议使用 cURL 和 file_get_contents 的帖子,但没有成功编辑它以用于我的网站。

这可能吗?

期待您的解答!

两个 cURL and file_get_contents() 都可以从 url 获得 完整的 html 输出 。例如 file_get_contents() 你可以这样做:

<?php

$content = file_get_contents('http://elssolutions.co.uk/about-els');
echo $content;

但是,如果您只需要页面的一部分,DOMDocument and DOMXPath 是更好的选择,对于后者,您还可以查询 DOM。下面是一个例子。

<?php

// The `id` of the node in the target document to get the contents of 
$url = 'http://elssolutions.co.uk/about-els';
$id = 'comp-iudvhnkb';


$dom = new DOMDocument();
// Silence `DOMDocument` errors/warnings on html5-tags
libxml_use_internal_errors(true);
// Loading content from external url
$dom->loadHTMLFile($url);
libxml_clear_errors();
$xpath = new DOMXPath($dom);

// Querying DOM for target `id`
$xpathResultset = $xpath->query("//*[@id='$id']")->item(0);

// Getting plain html
$content = $dom->saveHTML($xpathResultset);

echo $content;