One of my current clients (Project7) wanted a feed of Global News in their sidebar that pulled news articles from many different sources. RSS is perfect for this, but there’s a problem with most of the RSS plugins out there – they will pull the news articles into your site as blog posts. While this might not be a problem for some projects, when you’re dealing with hundreds of news articles a day you’ll need a solution that doesn’t store them locally.
Looping through an RSS Feed
You’ll want to place this code where you want the RSS feed to appear. In my case, this is the sidebar.php file.
include_once(ABSPATH . WPINC . '/feed.php');
$rss = fetch_feed('http://pipes.yahoo.com/pipes/pipe.run?_id=057832c1dcec7b587df982d689373923&_render=rss');
$maxitems = $rss->get_item_quantity(5);
$rss_items = $rss->get_items(0, $maxitems);
Here’s what it’s doing:
include_once(ABSPATH . WPINC . '/feed.php');– Pull in the the feed.php file which contains SimplePie.
$rss = fetch_feed('http://pipes.yahoo.com/pipes/pipe.run?_id=057832c1dcec7b587df982d689373923&_render=rss');– Fetch your feed. Set this to whatever RSS feed you want (as I mentioned, we’re using a Yahoo Pipe).
if(!empty($rss)):– If the RSS feed isn’t empty …
$maxitems = $rss->get_item_quantity(5); $rss_items = $rss->get_items(0, $maxitems); endif;– Set the limit to the 5 most recent items (you can set this to whatever you want).
That pulls in your RSS feed; now it’s time to do something with it. We’ll run it through a loop that displays the title (linked to the original permalink) and the summary. For a full list of what you can display, check the SimplePie Documentation.
First, the code:
<?php if ($maxitems == 0) echo '
- No news.
foreach ( $rss_items as $item ) : ?>
- get_permalink(); ?>" rel="bookmark">get_title(); ?> - get_description() ?>
Here’s what it’s doing:
<?php if ($maxitems == 0) echo '– If there’s nothing in the RSS feed, display “No News.” If there is posts in the RSS feed, run through them individually, assigning them to the variable $item.
- No news.
'; else foreach ( $rss_items as $item ) : ?>
- get_permalink(); ?>">get_title(); ?> -
- defines it as an item of the unordered list, bolds it, we’re then linking the title of the post to the permalink (the original source of the content), closing the link and the , then displaying the summary of the post and ending the list item.
– The endforeach ends the loop. When we’ve gone through all the post items, close the unordered list.
That’s pretty much it. The one on Project7′s site is a bit more complicated (we had to analyze the permalink to determine what kind of icon to display next to the post), but that’s how you display posts from an external RSS feed on your site, without storing those posts in your WordPress database.
Finally, I wanted to touch on Yahoo Pipes. If you haven’t used it before, it’s a great visual way for doing common programming mashups. Since we have 6 RSS feeds that we want included in this, and we want the 5 most recent posts from the total, I used Yahoo Pipes to combine these feeds into a single one. Below is a screenshot of my Pipe, along with a walkthrough of what it’s doing:
- First I created 6 “Fetch Feed” objects, and added the URL to the individual feeds to them.
- I then used the “Union” tool to group them together (you can only Union 5 objects, so I had to do two Unions with 3 objects each, then a Union on those two Unions)
- I then piped it through a “Sort” filter that sorts all the posts in descending order.
- I then limited the feed to 5 posts. While this might not be necessary for you since we were limiting the feed length above in WordPress (
$maxitems = $rss->get_item_quantity(5);), the final feed I pulled in had hundreds of posts and overloaded WordPress.
- I then sent this to the Pipe output.
Once you’ve saved the Pipe, you can go to Run Pipe, which will display the output. From there you have the option to “Get as RSS” – take that URL and paste it in the above code.
Bill Erickson is a WordPress Consultant who builds custom websites using WordPress as a CMS and Thesis or Genesis as a framework. He’s a cofounder and resident of The Creative Space, and a cofounder of the BIL Conference (the open analog to the TED Conference).