[WP]wordpressでRSSを取得して表示

wordpressにはRSSを取得して配列に整形する機能が備わってるらしい。…というのを最近TwitterAPI調べてた時知ったのでそれについてのメモ。
バージョンいくつからか詳しく確認してないんだけど、手元にあるので一番古いvar2.1.3には入ってた。
よく考えたらダッシュボードのRSSもコレだよな…ので、新しい情報ではありません。

外部サイトのRSSを取得するには次のように書く

include_once(ABSPATH . WPINC . '/rss.php');
$rss = fetch_rss($uri);

で、これだけ書いたら詳しい人にはティン!と来るかも。

使用されてるのはMagpieRSS and RSSCacheSnoopy HTTP client

たとえば、Twitterのコメントを取得する場合は次のようなソースになる。

include_once(ABSPATH . WPINC . '/rss.php')
$username ="hoge";
$twitter = fetch_rss('http://twitter.com/statuses/user_timeline/'.$username.'.rss');

fetch_rss()から返る$twitterは配列なので、foreachとかで個別処理すればおk。
RSSのXMLタグと配列のkeyがリンクしてます。

<ul>
<?php 
foreach ( $twitter->items as $message ) {
$msg = $message['description'];
print "<li>".$msg."</li>";
}
?>
</ul>

公式のリファレンスに直ぐ使えそうなサンプルソースがあったので
yahooのITmediaニュースを表示するように変更してみた(URLを)


<h2><?php _e('Headlines from Yahoo ITmedia News'); ?></h2>
<?php // Get RSS Feed(s)
include_once(ABSPATH . WPINC . '/rss.php');
$rss = fetch_rss('http://headlines.yahoo.co.jp/rss/itmedia_n.xml');
$maxitems = 5;
$items = array_slice($rss->items, 0, $maxitems);
?>
<ul>
<?php if (empty($items)) echo '<li>No items</li>';
else
foreach ( $items as $item ) : ?>
<li><a href='<?php echo $item&#91;'link'&#93;; ?>' 
title='<?php echo $item&#91;'title'&#93;; ?>'>
<?php echo $item&#91;'title'&#93;; ?>
</a></li>
<?php endforeach; ?>
</ul>

すごく…便利です…。

設定は、MagpieRSS and RSSCacheのやり方そのまま使えばおkらしい。

define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');//出力エンコード
define('MAGPIE_CACHE_DIR', './cache');//キャッシュディレクトリ
define('MAGPIE_FETCH_TIME_OUT', 30); //タイムアウトまでの時間
define('MAGPIE_CACHE_AGE', 60*60); // キャッシュ更新の時間(秒)。この場合1時間

もっと手軽に表示する

fetch_rss()は配列で返ってくるのでレイアウト変えたり出来るけど、
もっと簡単に取得表示するならwp_rss()かget_rss()が便利。

include_once(ABSPATH . WPINC . '/rss.php');
get_rss("http://wordpress.org/development/feed/");
wp_rss("http://wordpress.org/development/feed/");

include_once()とセットで使うのは変わらないが、上記のように書くだけで、それぞれ次のようなソースが出力される。
wp_rss()

<ul>
<li><a title="On Tuesday, May 27th, WordPress will turn 5 years old. We’ve come a long way from that original 0.7 release.
To celebrate we’re throwing a party in San Francisco at 111 Minna, starting at 9PM. You can get the full details and RSVP on Upcoming.org or on Facebook.
I hope you see some of you there, should &#91;...&#93;" href="http://wordpress.org/development/2008/05/birthday-party/">WordPress Birthday Party</a></li>
<li><a title="We’re doing some usability testing in New York City.  Join in if you’re in the area." href="http://wordpress.org/development/2008/05/usability-testing-in-new-york/">Usability Testing in New York</a></li>
<li><a title="WordCamps are my favorite events to go to because there’s something about the core WordPress community that attracts smart folks with good philosophies that are fun to hang out with. In this post I’ve collated the upcoming WordCamps we know about, including the one in San Francisco. Hopefully there will be one nearby so you &#91;...&#93;" href="http://wordpress.org/development/2008/04/upcoming-wordcamps/">Upcoming WordCamps</a></li>
<li><a title="Version 2.5.1 of WordPress is now available. It includes a number of bug fixes, performance enhancements, and one very important security fix. We recommend everyone update immediately, particularly if your blog has open registration. The vulnerability is not public but it will be shortly.
In addition to the security fix, 2.5.1 contains many bug fixes.  &#91;...&#93;" href="http://wordpress.org/development/2008/04/wordpress-251/">WordPress 2.5.1</a></li>
<li><a title="An Event Apart is a web design and development conference which features some of the same fine folks who helped out with WordPress 2.5. (And many others.) I attended the one in Chicago a while back and was engaged the whole day in interesting talks on design, writing copy as interface, advanced CSS, and creativity &#91;...&#93;" href="http://wordpress.org/development/2008/04/an-event-apart-discount/">An Event Apart Discount</a></li>
<li><a title="WordPress 2.5, the culmination of six months of work by the WordPress community, people just like you. The improvements in 2.5 are numerous, and almost entirely a result of your feedback: multi-file uploading, one-click plugin upgrades, built-in galleries, customizable dashboard, salted passwords and cookie encryption, media library, a WYSIWYG that doesn’t mess with your code, &#91;...&#93;" href="http://wordpress.org/development/2008/03/wordpress-25-brecker/">WordPress 2.5</a></li>
<li><a title="2.5 is coming along thanks to the fantastic feedback you guys provided on RC1 (over 580 pingbacks and counting), and we’re now ready to show you a bit more of a peek with a short screencast covering the new dashboard and uploader and Release Candidate 2. First here’s the screencast, which is also available embedded &#91;...&#93;" href="http://wordpress.org/development/2008/03/wordpress-25-rc2/">Screencast and WordPress 2.5 RC2</a></li>
<li><a title="A customizable dashboard, multi-file upload, built-in galleries, one-click plugin upgrades, tag management, built-in Gravatars, full text feeds, and faster load times sound interesting? Then WordPress 2.5 might be the release for you. It’s been in the oven for a while, and we’re finally ready to open the doors a bit to give you a taste.
For &#91;...&#93;" href="http://wordpress.org/development/2008/03/25-sneak-peek/">2.5 Sneak Peek</a></li>
<li><a title="WordPress 2.3.3 is an urgent security release. If you have registration enabled a flaw was found in the XML-RPC implementation such that a specially crafted request would allow a user to edit posts of other users on that blog.  In addition to fixing this security flaw,  2.3.3 fixes a few minor bugs.  &#91;...&#93;" href="http://wordpress.org/development/2008/02/wordpress-233/">WordPress 2.3.3</a></li>
<li><a title="WordPress 2.3.2 is an urgent security release that fixes a bug that can be used to expose your draft posts.  2.3.2 also suppresses some error messages that can give away information about your database table structure and limits and stops some information leaks in the XML-RPC and APP implementations.  Get 2.3.2 now to &#91;...&#93;" href="http://wordpress.org/development/2007/12/wordpress-232/">WordPress 2.3.2</a></li>
</ul>

get_rss()

<li>
<a href="http://wordpress.org/development/2008/05/birthday-party/" title="On Tuesday, May 27th, WordPress will turn 5 years old. We’ve come a long way from that original 0.7 release.
To celebrate we’re throwing a party in San Francisco at 111 Minna, starting at 9PM. You can get the full details and RSVP on Upcoming.org or on Facebook.
I hope you see some of you there, should &#91;...&#93;">WordPress Birthday Party</a><br>
</li>
<li>
<a href="http://wordpress.org/development/2008/05/usability-testing-in-new-york/" title="We’re doing some usability testing in New York City.&nbsp; Join in if you’re in the area.">Usability Testing in New York</a><br>
</li>
<li>
<a href="http://wordpress.org/development/2008/04/upcoming-wordcamps/" title="WordCamps are my favorite events to go to because there’s something about the core WordPress community that attracts smart folks with good philosophies that are fun to hang out with. In this post I’ve collated the upcoming WordCamps we know about, including the one in San Francisco. Hopefully there will be one nearby so you &#91;...&#93;">Upcoming WordCamps</a><br>
</li>
<li>
<a href="http://wordpress.org/development/2008/04/wordpress-251/" title="Version 2.5.1 of WordPress is now available. It includes a number of bug fixes, performance enhancements, and one very important security fix. We recommend everyone update immediately, particularly if your blog has open registration. The vulnerability is not public but it will be shortly.
In addition to the security fix, 2.5.1 contains many bug fixes.  &#91;...&#93;">WordPress 2.5.1</a><br>
</li>
<li>
<a href="http://wordpress.org/development/2008/04/an-event-apart-discount/" title="An Event Apart is a web design and development conference which features some of the same fine folks who helped out with WordPress 2.5. (And many others.) I attended the one in Chicago a while back and was engaged the whole day in interesting talks on design, writing copy as interface, advanced CSS, and creativity &#91;...&#93;">An Event Apart Discount</a><br>
</li>

引数とかは次の通り。

  • wp_rss($url, $num_items) ・・・ $urlは必須。$num_itemは取得表示数。デフォルトは全て表示
  • get_rss($url, $num_items) ・・・ $urlは必須。$num_itemは取得表示数。デフォルトは5

wp_rss()はulタグとセットで表示されるのに対し、get_rss()はリスト部分のみ出力されます。
また、取得できなかった場合、wp_rss()はエラーメッセージが出ますが、get_rss()はfalseが返されるだけです。

「[WP]wordpressでRSSを取得して表示」への27件のフィードバック

  1. ピンバック: [E2]e2esound.com
  2. ピンバック: Labrid WP
  3. ピンバック: umbrella_process blog

コメントを残す

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください