IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

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

    知更鸟发表于 2016-05-14 22:04:59
    love 0

    自动从其它站点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. ?>


沪ICP备19023445号-2号
友情链接