【问题标题】:Insert RSS Feeds as WP posts将 RSS 提要作为 WP 帖子插入
【发布时间】:2015-09-26 05:16:57
【问题描述】:

如何检索提要并使用 wp_insert_post() 插入它;如果该功能能够从一个独特的来源中为每个提要挑选 2 个提要,我会非常感兴趣。

【问题讨论】:

    标签: php xml wordpress


    【解决方案1】:

    如果您能够编写 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 );
    

    根据需要进行调整以满足您的需要。

    希望对您有所帮助,它会给您比插件更多的控制权。

    【讨论】:

    • 谢谢@Simon,这似乎更容易。让我试一试
    • 祝你好运 - 如果您有任何问题或需要任何进一步的帮助,请告诉我
    猜你喜欢
    • 2015-01-10
    • 2020-01-25
    • 2019-05-17
    • 1970-01-01
    • 2012-12-24
    • 2023-03-10
    • 2015-05-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多