Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
10
Help us understand the problem. What is going on with this article?
@konweb

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

More than 5 years have passed since last update.

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
Help us understand the problem. What is going on with this article?
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
konweb

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
10
Help us understand the problem. What is going on with this article?