LoginSignup
10
10

More than 5 years have passed since last update.

【WordPress】他のブログ記事をRSSで取得する

Last updated at Posted at 2014-07-12

WordPressにRSSを取得するfetch_feedという関数があるので、これを使う。
こんな感じ

hoge.php
<?php
    include_once(ABSPATH . WPINC . '/feed.php');
    $rss_url = array(
      'RSS_url'
    );

    $rss = fetch_feed($rss_url);
    if (!is_wp_error($rss)){
      $maxitems = $rss->get_item_quantity(5);
      $rss_items = $rss->get_items(0, $maxitems);
    }

    foreach($rss_items as $key => $value){
      $first_img = '';
      if ( preg_match( '/<img.+?src=[\'"]([^\'"]+?)[\'"].*?>/msi', $value->get_content(), $matches ) ) {
        $first_img = $matches[1];
      }

      echo '
      <li style="margin-bottom: 20px;">
        <img src="'.esc_attr( $first_img ).'" width="100" height="100">
        <a href="'.$value->get_permalink().'" target="_blank">'.$value->get_title().'</a>
      </li>
      ';
    }
?>

取得件数を変えるにはget_item_quantity(5)の数字を変更する

10
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
10