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

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

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

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

PHP:
  1. include_once(ABSPATH . WPINC . '/rss.php');
  2. $rss = fetch_rss($uri);

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

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

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

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

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

PHP:
  1. <ul>
  2. <?php
  3. foreach ( $twitter->items as $message ) {
  4. $msg = $message['description'];
  5. print "<li>".$msg."</li>";
  6. }
  7. ?>
  8. </ul>

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

PHP:
  1. <h2><?php _e('Headlines from Yahoo ITmedia News'); ?></h2>
  2. <?php // Get RSS Feed(s)
  3. include_once(ABSPATH . WPINC . '/rss.php');
  4. $rss = fetch_rss('http://headlines.yahoo.co.jp/rss/itmedia_n.xml');
  5. $maxitems = 5;
  6. $items = array_slice($rss->items, 0, $maxitems);
  7. ?>
  8. <ul>
  9. <?php if (empty($items)) echo '<li>No items</li>';
  10. else
  11. foreach ( $items as $item ) : ?>
  12. <li><a href='<?php echo $item['link']; ?>'
  13. title='<?php echo $item['title']; ?>'>
  14. <?php echo $item['title']; ?>
  15. </a></li>
  16. <?php endforeach; ?>
  17. </ul>

すごく…便利です…。

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

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

もっと手軽に表示する

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

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

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

PHP:
  1. <ul>
  2. <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.
  3. 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.
  4. I hope you see some of you there, should [...]" href="http://wordpress.org/development/2008/05/birthday-party/">WordPress Birthday Party</a></li>
  5. <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>
  6. <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 [...]" href="http://wordpress.org/development/2008/04/upcoming-wordcamps/">Upcoming WordCamps</a></li>
  7. <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.
  8. In addition to the security fix, 2.5.1 contains many bug fixes.  [...]" href="http://wordpress.org/development/2008/04/wordpress-251/">WordPress 2.5.1</a></li>
  9. <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 [...]" href="http://wordpress.org/development/2008/04/an-event-apart-discount/">An Event Apart Discount</a></li>
  10. <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, [...]" href="http://wordpress.org/development/2008/03/wordpress-25-brecker/">WordPress 2.5</a></li>
  11. <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 [...]" href="http://wordpress.org/development/2008/03/wordpress-25-rc2/">Screencast and WordPress 2.5 RC2</a></li>
  12. <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.
  13. For [...]" href="http://wordpress.org/development/2008/03/25-sneak-peek/">2.5 Sneak Peek</a></li>
  14. <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.  [...]" href="http://wordpress.org/development/2008/02/wordpress-233/">WordPress 2.3.3</a></li>
  15. <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 [...]" href="http://wordpress.org/development/2007/12/wordpress-232/">WordPress 2.3.2</a></li>
  16. </ul>

get_rss()

PHP:
  1. <li>
  2. <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.
  3. 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.
  4. I hope you see some of you there, should [...]">WordPress Birthday Party</a><br>
  5. </li>
  6. <li>
  7. <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>
  8. </li>
  9. <li>
  10. <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 [...]">Upcoming WordCamps</a><br>
  11. </li>
  12. <li>
  13. <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.
  14. In addition to the security fix, 2.5.1 contains many bug fixes.  [...]">WordPress 2.5.1</a><br>
  15. </li>
  16. <li>
  17. <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 [...]">An Event Apart Discount</a><br>
  18. </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が返されるだけです。

このエントリをはてなブックマークに追加このエントリをdel.icio.usに追加このエントリをLivedoor Clipに追加このエントリをYahoo!ブックマークに追加このエントリをFC2ブックマークに追加このエントリをNifty Clipに追加このエントリをPOOKMARK. Airlinesに追加このエントリをBuzzurl(バザール)に追加このエントリをChoixに追加このエントリをnewsingに追加

Comments:0

Comment Form
Remember personal info

Trackbacks:16

Trackback URL for this entry
http://tenderfeel.xsrv.jp/wordpress/197/trackback/
Listed below are links to weblogs that reference
[WP]wordpressでRSSを取得して表示 from WebTecNote
pingback from [E2]e2esound.com 08-07-08 (火) 20:31

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

pingback from genulabelog - tumblrのフィードを取得 08-11-05 (水) 18:22

[...] MagpieRSS(フィードのitems要素を配列に整形) [...]

pingback from トップページにdeliciousブックマーク+コメント表示成功!!! » みきょうやのぶろぐ 08-12-20 (土) 19:28

[...] まずWordPress でトップページ専用のテンプレートを作成する覚え書き | [k]idこちらのサイトでdeliciousブックマーク表示部を分離させてやるところから始めたんだけど現在使用しているテーマだと<?php~?>でくくってやらないと認識しないところでまずつまづいてそれが終わったらこちらを参考にして[WP]wordpressでRSSを取得して表示 - WebTecNoteみるもget_rssやwp_rssコマンドで収得したRSSがなぜか文字化けしてしまう問題にぶち当たり結局、用意された便利関数を使わず同じページの「yahooのITmediaニュースを表示するように変更してみた」を例にして打ち込んでやると文字化けの問題は解消ブックマーク時に書き込んだコメントも表示させたくていろいろ試すとecho $item['summary']でコメントが表示されることを確認(これは$item['description']でも表示できるかも)ここまででだいたいやりたいことはできあがったんだが現在サイドバーに表示させてるdeliciousのfeedだと日付が表示できるんですよね~ということで日付の表示の仕方を四苦八苦いったいどこに格納されてるんだ~とrss.phpのソースを開いてみたりしたものの見つからずんでdeliciousのfeedのソースを開いてみると日付にpubDateのデータ名が!これだーとばかりに早速適応してみるもそもそも構造自体をよくわかってなかったので肝心のデータの格納されている場所がわからず本当にデータとして取り込んでいるんだろうか?と悩みながらもようやく表示に成功!!そして同じくこちらの[PHP]サイト内のページに外部のRSSを表示する - WebTecNote日時の整形を参考にして年月日の形への表示も成功!やっと満足のゆく形にできたぞーーー!!! [...]

pingback from ミニブログのRSSを取得しWordPress のブログに表示させる - まったりなぶろぐ 09-02-18 (水) 11:29

[...] 以前から、こんな事が出来るだろうなと思いつつ機会がながったが、ミニブログを開設したきっかけで、挑戦してみた。 参考にした所は下記 WordPressで外部RSSを取得して表示させる [WP]wordpressでRSSを取得して表示 [...]

pingback from みっくすじゅーす » Exec-PHP が WordPress 2.7 対応になりました。 09-02-24 (火) 18:02

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

pingback from wordpressでRSSを取得して表示 « テスト2 09-03-30 (月) 11:04

[...] WebTecNote さんのサイトから [...]

trackback from Labrid WP 09-03-31 (火) 20:19

Wordpress サイドバーに外部RSS を読み込んで表示する

Pages コンテンツ(上部ナビゲーションのリンク先)のサイドバーに、
外部RSS(私YAMADAの個人ブログ)を読み込むようカスタマイズしました。
RSS読み込みプラグイン(ex. MagpieRSS )で実現…

pingback from Moo Doo Blog - wordpressでRSS表示のためのサンプルコード 09-04-12 (日) 2:28

[...] 参考:http://tenderfeel.xsrv.jp/wordpress/197/ [...]

trackback from umbrella_process blog 09-05-20 (水) 17:58

[WordPress] 他サイトのRSSを読み込んで表示

WordPressはPHPでできているので、他サイトのRSSを読み込ませてサイドバーなどに新着記事を表示するのも簡単だ…と思っていたら意外と手こずったのでメモ。
いくつかプラグインも配布さ…

pingback from Pluginなしで外部RSSを取得して表示させる方法|WordPress情報 09-09-14 (月) 12:57

[...] <参考> →WebTecNote バージョンが古ければ以下でも可のはずです。 [...]

pingback from wordpressカスタマイズのメモ1 · Ribbit 09-10-04 (日) 11:27

[...] WebTecNote - [WP]wordpressでRSSを取得して表示 http://tenderfeel.xsrv.jp/wordpress/197/ <wordpressにはRSSを取得して配列に整形する機能が備わってるらしい。…というのを最近TwitterAPI調べてた時知ったの [...]

pingback from WorsPressで他ブログのRSSを表示する – waratsuyoshi.jp 09-10-28 (水) 21:38

[...] 参考にしたのは WordPressで外部RSSを取得して表示させる [WP]wordpressでRSSを取得して表示 [...]

pingback from スタッフブログ|dis-ドアズインターネットサービス|WEB(ホームページ)制作、作成・SEO・FLASH・ビジネスブログ・ユーザビリティ・アクセシビリティ|神戸・大阪 09-10-29 (木) 13:49

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

pingback from 米が好き| 【WordPress】外部RSSを取得して表示する方法 09-11-18 (水) 0:19

[...] 利用させてもらったのは、以下のサイトからです。 WebTecNote [WP]wordpressでRSSを取得して表示 [...]

pingback from 外部RSSを表示する « WordPressテストサイト 09-12-16 (水) 19:32

[...] とっても、分かりやすく書かれたサイトを見つけたの早速やってみました。 http://tenderfeel.xsrv.jp/wordpress/197/ 私のやっているFC2ブログ「未来予想図」でやってみました。 <?php include_onc [...]

trackback from Web制作日記 - Web Diary.net 10-01-23 (土) 1:59

Delicious × HeartRails Captureで作るBookmark

本日より新たなコンテンツとしてBookmarkページを追加しました。 このブックマークはソーシャルブックマークのDeliciousでブックマークした最新20件を自動的にWeb Diary.netに表示されるようRSS…

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

最近の投稿
最近の修正
  • そういえばまだMooToolsのフォームバリデーターについて書いてない 14 hrs回前
  • Chromeが早速赤さんのブログトップをキャプしなすった。まさに外道。 19 hrs回前
  • 月額98円てww http://www.quicca.com/ 19 hrs回前
  • 若いおなごにみつぐおやじたちの気持ち…今なら分かる!! 1 day回前
  • 誕生日プレゼントにヒャッホイする妹かわゆす 1 day回前
  • More updates...

Powered by Twitter Tools

Tag Cloud
Tool&AD
メタ情報

Return to page top