【发布时间】:2015-09-26 05:16:57
【问题描述】:
如何检索提要并使用 wp_insert_post() 插入它;如果该功能能够从一个独特的来源中为每个提要挑选 2 个提要,我会非常感兴趣。
【问题讨论】:
如何检索提要并使用 wp_insert_post() 插入它;如果该功能能够从一个独特的来源中为每个提要挑选 2 个提要,我会非常感兴趣。
【问题讨论】:
如果您能够编写 PHP 代码,则可以轻松循环通过 RSS 提要并将数据插入 WordPress。我通常会创建一个新的帖子类型(当然你可以使用默认的“帖子”类型)。在下面的示例中,我使用了我制作的名为“文章”的帖子类型。
有很多方法可以循环浏览 RSS 提要,这是我使用的方法:
$rss = new DOMDocument();
$rss->load($rss_url);
// Loop through each item in the feed
foreach ($rss->getElementsByTagName('item') as $node) {
// Code goes here
// Example to get a value
// Define a namespace
$ns = 'http://purl.org/rss/1.0/modules/content/';
$content = $node->getElementsByTagNameNS($ns, 'encoded');
$content = $content->item(0)->nodeValue;
}
在循环中通过您的 RSS 提要获取数据并保存为变量,然后运行以下命令:
$new_article = array(
'post_title' => $title,
'post_content' => $content,
'post_excerpt' => $description,
'post_type' => 'article',
'post_date' => date('Y-m-d H:i:s',strtotime($date)),
'post_author' => 1,
'post_status' => 'publish'
);
wp_insert_post( $new_article , true );
根据需要进行调整以满足您的需要。
希望对您有所帮助,它会给您比插件更多的控制权。
【讨论】: