插入RSS源作为WP帖子


Insert RSS Feeds as WP posts

如何检索提要&使用wp_insert_post()插入;我非常感兴趣,如果该函数可以从一个唯一的来源挑选2个提要,每个提要。

如果您能够编写PHP代码,则很容易遍历RSS提要并将数据插入WordPress。我通常创建一个新的帖子类型(当然你可以使用默认的"post"类型)。在下面的例子中,我使用的是我创建的文章类型article。

有很多方法可以循环浏览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 );

根据需要调整

希望这对你有帮助,它会给你更多的控制比插件。