10
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

posted at

updated at

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

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)の数字を変更する

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Sign upLogin
10
Help us understand the problem. What are the problem?