1

我有一个朋友写了几个不同的博客,想从一个不太受欢迎的博客中提取故事,到他的主博客上,并带有一个 rss 提要并显示其中的图像(因为 rss 提要有时有图像) .

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

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

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

4

4 回答 4

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 类来获取 url 并将其上传到您的服务器,以便您可以将其设置为特色图像。

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

于 2012-11-13T08:20:53.647 回答
1

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

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

于 2013-12-04T00:55:49.643 回答
0

不幸的是,我不知道有什么办法,但是您是否考虑过修改插件?周围有大量的内容管理插件(feedwordpress、autoblog 等)。您可能会在wp_insert_post()某处找到该行并对其进行修改以包含您的自定义帖子类型/分类法。

编辑 自己跳进插件(feedwordpress),所有的insert_post东西都在里面syndicatedpost.class.php——第wp_insert_post()1538行的主要内容

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

于 2012-11-13T00:33:48.097 回答
0

嘿,为什么不试试这个http://wordpress.org/extend/plugins/display-latest-rss-feeds/ 它会从任何帐户中提取 rss 源并将其显示到您的博客。不幸的是,它不会仅显示 rss 提要标题及其指向原始博客的永久链接的图像,但您可以根据需要轻松修改源代码。

于 2012-11-13T00:57:43.717 回答