自动从其它站点RSS抓取文章

WordPress308.3K阅读模式

自动从其它站点RSS抓取文章

代码基本原理,新建一个自定义分类法“shot”,自动从另一个站点的RSS抓取文章,放在一个数组中,再用这个数组创建“shot”文章,并为文章添加自定义栏目image和link_url,方便调用缩略图和原文链接。

将下面代码添加到当前主题functions.php中即可。

  1. <?
  2. /* Post Type
  3.    ------------------------------------------------------------------------------------ */
  4. add_action( 'init', 'register_cpt_shot' );
  5. function register_cpt_shot() {
  6.     $labels = array(
  7.         'name' => _x( 'Dribble Shots', 'shot' ),
  8.         'singular_name' => _x( 'Dribbble Shot', 'shot' ),
  9.         'add_new' => _x( 'Add New', 'shot' ),
  10.         'add_new_item' => _x( 'Add New Dribbble Shot', 'shot' ),
  11.         'edit_item' => _x( 'Edit Dribbble Shot', 'shot' ),
  12.         'new_item' => _x( 'New Dribbble Shot', 'shot' ),
  13.         'view_item' => _x( 'View Dribbble Shot', 'shot' ),
  14.         'search_items' => _x( 'Search Dribble Shots', 'shot' ),
  15.         'not_found' => _x( 'No dribble shots found', 'shot' ),
  16.         'not_found_in_trash' => _x( 'No dribble shots found in Trash', 'shot' ),
  17.         'parent_item_colon' => _x( 'Parent Dribbble Shot:', 'shot' ),
  18.         'menu_name' => _x( 'Dribble Shots', 'shot' ),
  19.     );
  20.     $args = array(
  21.         'labels' => $labels,
  22.         'hierarchical' => false,
  23.         'supports' => array( 'title', 'custom-fields' ),
  24.         'public' => true,
  25.         'show_ui' => true,
  26.         'show_in_menu' => true,
  27.         'show_in_nav_menus' => false,
  28.         'publicly_queryable' => true,
  29.         'exclude_from_search' => false,
  30.         'has_archive' => true,
  31.         'query_var' => true,
  32.         'can_export' => true,
  33.         'rewrite' => true,
  34.         'capability_type' => 'post'
  35.     );
  36.     register_post_type( 'shot', $args );
  37. }
  38. /* Import Shots via RSS
  39.    ------------------------------------------------------------------------------------ */
  40. // grab the image src from teh description
  41. function get_image($string) {
  42.     preg_match_all('/<img[^>]+>/i',$string$result);
  43.     $img = array();
  44.     foreach$result[0] as $img_tag)
  45.     {
  46.         preg_match_all('/(src)=("[^"]*")/i',$img_tag, $img[$img_tag]);
  47.     }
  48.     return trim($img[$img_tag][2][0], '"');
  49. }
  50. // create an array of the feed items
  51. include_once(ABSPATH . WPINC . '/feed.php');
  52. $feed = fetch_feed('http://dribbble.com/tammyhart/shots.rss');
  53. $feed = $feed->get_items(0);
  54. $shots = array();
  55. foreach ( $feed as $item ) :
  56. $shots[$item->get_date('Ymd')] = array(
  57.     'id'    => $item->get_date('Ymd'),
  58.     'url'   => esc_url( $item->get_permalink() ),
  59.     'date'  => $item->get_date('Y-m-d H:i:s'),
  60.     'title' => esc_html( $item->get_title() ),
  61.     'image' => get_image($item->get_description())
  62.     );
  63. endforeach;
  64. // create posts from our array
  65. foreach ($shots as $shot) {
  66.     $shot_post = array(
  67.         'post_type'     => 'shot',
  68.         'post_status'   => 'publish',
  69.         'post_author'   => 1,
  70.         'post_title'    => $shot['title'],
  71.         'post_date'     => $shot['date']
  72.         );
  73.     $shot_post_meta = array(
  74.         'link_url'  => $shot['url'],
  75.         'image'     => $shot['image']
  76.         );
  77.     $posts = get_posts(
  78.         array(
  79.             'post_type' => 'shot',
  80.             'meta_key'  => 'link_url',
  81.             'meta_value'=> $shot_post_meta['link_url']
  82.             )
  83.         );
  84.     if (count($posts) == 0) {
  85.         $post_id = wp_insert_post($shot_post);
  86.         add_post_meta($post_id, 'link_url', $shot_post_meta['link_url'], true);
  87.         add_post_meta($post_id, 'image', $shot_post_meta['image'], true);
  88.     }
  89. }
  90. ?>

修改第66行的”http://dribbble.com/tammyhart/shots.rss“为准备抓取文章站点的RSS地址,还可以修改第80行后的”shot“为你所用主题自定义分类法名称,抓取的文章会自动添加到该分类法中。

上述代码中并没有抓取文章内容的功能,仅供参考学习之用。

本站文章大部分始于原创,用于个人学习记录,可能对您有所帮助,仅供参考!

weinxin
我的微信
版权声明
本站原创文章转载请注明文章出处及链接,谢谢合作!
 
知更鸟
评论  30  访客  30
    • 云落
      云落 2

      类似采集是吧

      • 星岩博客
        星岩博客 2

        有了这段代码,不用自己写代码,就能给自己的网站添加这一功能了。

        • Koolight
          Koolight 4

          这个相当于采集了?

          • boke112导航
            boke112导航 5

            有了这个功能,感觉可以聚合一些比较多干货文章的站点资源

            • 软膜天花
              软膜天花 4

              测试一下,合不适我的网站用!

              • 墨丶水瓶
                墨丶水瓶 4

                :grin: 这是一个邪恶的功能

                • hi
                  hi 0

                  你好,GK

                  • 通宝娱乐下载
                    通宝娱乐下载 1

                    博主确实太厉害了,只能膜拜了!

                    • 叮咚博客
                      叮咚博客 0

                      这个可以有哦!

                      • 百度
                        百度 1

                        发的郭德纲

                        • fsfs
                          fsfs 1

                          ffffffffffffffffffffffffdsfs发的方法是打发

                          • 李光春
                            李光春 2

                            这功能不错

                            • baojhud
                              baojhud 0

                              安防V的发挥个人与他人色儿

                              • 钊林IT
                                钊林IT 3

                                这个功能有点类似如果可以获取到他人网站对应文章链接和文章标题,感觉更神奇(不涉及版权情况下),将同类优质的文章推荐给更多人阅读。。

                                • 电我
                                  电我 2

                                  怎么抓取多个网站的内容?

                                  • 哈哈大笑
                                    哈哈大笑 0

                                    请问博主,怎样才能像你这个发布文章的时候插入代码块。是wordpress的。

                                    • 恒创主机7折
                                      恒创主机7折 5

                                      好强大的功能

                                      • 恒创主机7折
                                        恒创主机7折 5

                                        呵呵 下一个版本 集合到主题里面去

                                        • 乐乎哉
                                          乐乎哉 1

                                          这个方法好。避免使用插件,给网站减轻负担,又能实现功能,挺好的。

                                          • 幅度高达
                                            幅度高达 0

                                            持续长

                                            • 快启动论坛
                                              快启动论坛 3

                                              这个厉害了,要是能抓内容,完整版的就更好了

                                              • hostus
                                                hostus 0

                                                这个功能挺好的,适合聚合内容。如果能多个RSS资源目标一起就好了。

                                                • Dazi
                                                  Dazi 2

                                                  测试一下看看

                                                  • 乐赚网
                                                    乐赚网 1

                                                    给力

                                                    • 文
                                                      0

                                                      博主的渐显是怎么实现的

                                                      • 元龙
                                                        元龙 1

                                                        有了这个,可以自动化建站了。

                                                        • 优维国际
                                                          优维国际 0

                                                          确实不错。实用

                                                        匿名

                                                        发表评论

                                                        匿名网友
                                                        :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

                                                        拖动滑块以完成验证