Wordpress rss作为帖子类型提供图像/永久链接

时间:2012-11-13 00:11:28

标签: wordpress rss

我有一位朋友写了几个不同的博客,并希望将故事从一个不那么受欢迎的博客拉到他的主博客上,并带有rss feed并显示来自它的图像(因为rss feed有图像) ,有时)。

显示rss feed不应该太难,它会使它们成为一种对我来说似乎更难的自定义帖子类型。

如果有人有任何想法,请拍摄。

编辑: - 有谁知道如何让外部RSS提要在wordpress中显示为自定义帖子类型?

4 个答案:

答案 0 :(得分:4)

一种简单的方法可能是使用Wordpress自己的fetch_feed函数: http://codex.wordpress.org/Function_Reference/fetch_feed

一个简单示例(假设您已经设置了自定义帖子类型):

function import_feed_items()
{
  $feed = fetch_feed('http://feeds.bbci.co.uk/news/uk/rss.xml');

  if( !is_wp_error($feed) )
  {

    if( $last_import = get_option('last_import') )
    {
      $last_import_time = $last_import;
    } else {
      $last_import_time = false;
    }

    $items = $feed->get_items();
    $latest_item_time = false;

    foreach ( $items as $item )
    {

      $item_date = $item->get_date('Y-m-d H:i:s');
      if( $last_import_time && ($last_import_time >= strtotime($item_date)) )
      {
        continue;
      }

      $post = array(
        'post_content'   => $item->get_content(),
        'post_date'      => $item_date,
        'post_title'     => $item->get_title(),
        'post_status'    => 'publish',
        'post_type'      => 'custom_post_type'
      );
      wp_insert_post($post);

      if( strtotime($item_date) > $latest_item_time )
      {
        $latest_item_time = strtotime($item_date);
      }

    }

    if( false !== $latest_item_time )
    {
      update_option('last_import', $latest_item_time);
    }

  }
  else
  {
    echo $feed->get_error_message();
  }
}
add_action('wp', 'import_feed_items');

如果内容中有图像标记,您可以使用php的DomDocument类获取网址并将其上传到您的服务器,以便将其设置为特色图片。

http://codex.wordpress.org/Function_Reference/wp_insert_attachment

http://codex.wordpress.org/Function_Reference/set_post_thumbnail

修改

更正了时间戳检查。此更新示例使用'wp'挂钩运行,以便您可以更快地查看结果。最好将其设置为cron任务。见http://codex.wordpress.org/Function_Reference/wp_schedule_event

答案 1 :(得分:1)

Feed to Post插件是将多个RSS项目导入WordPress博客的完美解决方案,您甚至可以将它们存储为帖子或自定义帖子类型。

http://www.wprssaggregator.com/extension/feed-to-post/

答案 2 :(得分:0)

不幸的是,我不知道有什么办法,但是你看过修改一个插件吗?有大量的内容策展插件(feedwordpress,autoblog等)。您可以在某处找到wp_insert_post()行并修改它以包含您的自定义帖子类型/分类。

修改 我自己跳转到插件(feedwordpress),所有insert_post内容都在syndicatedpost.class.php - 第1538行的主wp_insert_post()

编辑如果您在阅读该插件代码时想要傻笑,您会发现很多 f字的实例 ...哈哈

答案 3 :(得分:0)

嘿,为什么不尝试这个http://wordpress.org/extend/plugins/display-latest-rss-feeds/ 它会从任何帐户中提取RSS Feed并将其显示在您的博客中。不幸的是,它不会显示图像只是rss feed标题及其与原始博客的永久链接,但如果你愿意,你可以轻松修改源代码。