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

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

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

  1. <?
  2. add_action( \’init\’, \’register_cpt_shot\’ );
  3.  register_cpt_shot() {
  4.      = (
  5.         \’name\’ => _x( \’Dribble Shots\’, \’shot\’ ),
  6.         \’singular_name\’ => _x( \’Dribbble Shot\’, \’shot\’ ),
  7.         \’add_new\’ => _x( \’Add New\’, \’shot\’ ),
  8.         \’add_new_item\’ => _x( \’Add New Dribbble Shot\’, \’shot\’ ),
  9.         \’edit_item\’ => _x( \’Edit Dribbble Shot\’, \’shot\’ ),
  10.         \’new_item\’ => _x( \’New Dribbble Shot\’, \’shot\’ ),
  11.         \’view_item\’ => _x( \’View Dribbble Shot\’, \’shot\’ ),
  12.         \’search_items\’ => _x( \’Search Dribble Shots\’, \’shot\’ ),
  13.         \’not_found\’ => _x( \’No dribble shots found\’, \’shot\’ ),
  14.         \’not_found_in_trash\’ => _x( \’No dribble shots found in Trash\’, \’shot\’ ),
  15.         \’parent_item_colon\’ => _x( \’Parent Dribbble Shot:\’, \’shot\’ ),
  16.         \’menu_name\’ => _x( \’Dribble Shots\’, \’shot\’ ),
  17.     );
  18.      = (
  19.         \’labels\’ => ,
  20.         \’hierarchical\’ => false,
  21.         \’supports\’ => ( \’title\’, \’custom-fields\’ ),
  22.         \’\’ => true,
  23.         \’show_ui\’ => true,
  24.         \’show_in_menu\’ => true,
  25.         \’show_in_nav_menus\’ => false,
  26.         \’publicly_queryable\’ => true,
  27.         \’exclude_from_search\’ => false,
  28.         \’has_archive\’ => true,
  29.         \’query_var\’ => true,
  30.         \’can_export\’ => true,
  31.         \’rewrite\’ => true,
  32.         \’capability_type\’ => \’post\’
  33.     );
  34.     register_post_type( \’shot\’,  );
  35. }
  36.  get_image() {
  37.     preg_match_all(\’/<img[^>]+>/i\’,, );
  38.      = ();
  39.     ( [0]  )
  40.     {
  41.         preg_match_all(\’/(src)=(]*
  42. \’);
  43. }
  44. (ABSPATH . WPINC . \’/feed.php\’);
  45.  = fetch_feed(\’http:
  46.  = ->get_items(0);
  47.  = ();
  48.  (    ) :
  49. [->get_date(\’Ymd\’)] = (
  50.     \’id\’    => ->get_date(\’Ymd\’),
  51.     \’url\’   => esc_url( ->get_permalink() ),
  52.     \’\’  => ->get_date(\’Y-m-d H:i:s\’),
  53.     \’title\’ => esc_html( ->get_title() ),
  54.     \’image\’ => get_image(->get_description())
  55.     );
  56. ;
  57.  (  ) {
  58.      = (
  59.         \’post_type\’     => \’shot\’,
  60.         \’post_status\’   => \’publish\’,
  61.         \’post_author\’   => 1,
  62.         \’post_title\’    => [\’title\’],
  63.         \’post_date\’     => [\’\’]
  64.         );
  65.      = (
  66.         \’link_url\’  => [\’url\’],
  67.         \’image\’     => [\’image\’]
  68.         );
  69.      = get_posts(
  70.         (
  71.             \’post_type\’ => \’shot\’,
  72.             \’meta_key\’  => \’link_url\’,
  73.             \’meta_value\’=> [\’link_url\’]
  74.             )
  75.         );
  76.      (() == 0) {
  77.          = wp_insert_post();
  78.         add_post_meta(, \’link_url\’, [\’link_url\’], true);
  79.         add_post_meta(, \’image\’, [\’image\’], true);
  80.     }
  81. }
  82. ?>

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

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

    © 版权声明
    THE END
    喜欢就支持一下吧
    点赞0 分享
    评论 抢沙发

    请登录后发表评论

      暂无评论内容