Add latest RSS items to Wordpress Post
If you’re using Wordpress to build a website showcase (or in my case blog showcase), it’s nice to show the recent posts from that website in the showcase.
In this tutorial I’m going to show you how to do this, using the Wordpress Custom Fields feature and SimplePie.
Read more after the jump.
Step 1: Custom Fields
Wordpress allows you to add custom data for your blog posts. This allows you to use Wordpress for much more than just blogs. In this case we’re going to use the feature to store the URL for the RSS feed of the website we’re showcasing.
Create a new blog post. In the edit screen towards the bottom of the page you should see an option for “Custom Fields” (you may need to expand this section, by click on the plus icon).

If you’ve used Custom Fields before you’ll get a drop down of field names you’ve previously used, as well as way to add a new one.
We’ll use the name ‘rss_url’ for the key, and for the value enter the URL of the RSS feed you want to include in the post.

All done with Custom Fields.
Step 2: Install SimplePie
SimplePie is a PHP library for the interacting with RSS feeds. It was designed to be simple enough for non-programmers to use, but powerful and extendable for the pros.
Download the latest version of the library from the official website, as of writing, the current version is 1.1.
Upload the file to your server, I took the opportunity to rename the file adding a .php extension. I put the file into a folder called classes in my Wordpress root folder. This is usually the root of your website, but if you’re running Wordpress from a subfolder upload SimplePie there.
Step 3: Access the RSS feed
Now we need to tell Wordpress to access the RSS feed.
To keep thing simple, I decided to just show the posts, but not actually store them in Wordpress. We’re going to modify the single.php file in your theme to use the Custom Field and Simple Pie to access the RSS feed.
<?php
require('classes/simplepie.inc.php');
$customFields = get_post_custom();
if (!empty($customFields['rss_url'][0])) {
$feed = new SimplePie();
$feed->set_feed_url($customFields['rss_url'][0]);
$feed->init();
$feed->handle_content_type();
}
?>
Here we check to see if the rss_url value is set as a custom field, and if it is, we initialize the SimplePie class that will access the RSS feed.
Caching
To avoid having to fetch the RSS feed each time the post is displayed to a visitor, you can make use of SimplePie’s caching. SimplePie will store a copy of the RSS feed on your server for a set time period (default is 20 minutes). This feature is activated by default, but does require that you create a folder called “cache” in your Wordpress home directory which the webserver has write permissions on. How to do this is different from host to host, please check with your host for instructions.
Step 4: Display the feed
Now that we have the item list from the RSS feed we need to display them. Inside the if statement we wrote in Step 3, add the following code. (Complete Listing below).
<?php if ($feed->data) { ?>
<h3>Recent Posts From This Blog</h3>
<ul class="recentPosts">
<?php $items = $feed->get_items(0, 5); ?>
<?php foreach($items as $item) { ?>
<li>
<a href="<?php echo $item->get_permalink(); ?>">
<?php echo $item->get_title(); ?>
</a>
</li>
<?php } ?>
</ul>
<?php } ?>
Here we check to see if data was successfully loaded by SimplePie, and then we loop thru the results. The parameters to the get_items function specify where to start in the fetch and how many items to return. In this case we’re asking for the first 5 items.
Step 5: Style the display
Depending on your theme you may not need to change anything here, as your css may contain the necessary styling commands already.
Using CSS we’re able to style the feed items.
The items you’ll want to add styling for if needed are:
ul.recentPosts {}
ul.recentPosts li {}
ul.recentPosts li a {}
Mine looks this:

Step 6: You’e done!
Thats is it. From now on when your post detail page is viewed you’ll be able to see a list of the current posts from that site. To see this in action check out the I Heart Blogs website.
<?php
require('classes/simplepie.inc.php');
$customFields = get_post_custom();
if (!empty($customFields['rss_url'][0])) {
$feed = new SimplePie();
$feed->set_feed_url($customFields['rss_url'][0]);
$feed->init();
$feed->handle_content_type();
if ($feed->data) { ?>
<h3>Recent Posts From This Blog</h3>
<ul class="recentPosts">
<?php $items = $feed->get_items(0, 5); ?>
<?php foreach($items as $item) { ?>
<li>
<a href="<?php echo $item->get_permalink(); ?>">
<?php echo $item->get_title(); ?>
</a>
</li>
<?php } ?>
</ul>
<?php } ?>
<?php } ?>
Tags: RSS, SimplePie, Wordpress
February 19th, 2008 at 2:26 pm
i thought WP had a rss reader built into it?
February 19th, 2008 at 2:42 pm
@Jenny: No, I don’t think so. There are a number of plugins that let you add RSS feeds to the sidebar and such, and to create posts like I’ve done on this site. They all use external libraries (SimplePie, and Magpie are 2 popular ones).
I take that back, a modified version of Magpie is included with Wordpress, but I find that SimplePie is much easier to work with and more robust at parsing the RSS feeds.
March 1st, 2008 at 10:00 am
Hi, since PHP5 there is no need to use and external library. So another approach could be to use the PHP function simplexml_load_file.
Using your example:
<?php
// Get the first value for rss_url
$values = get_post_custom_values(”rss_url”);
$rss_url = $values[0];
if ($rss_url) {
// Load the XML file into a Simple XML object
$feed = simplexml_load_file($rss_url);
if ($feed) {
echo “Recent Posts From This Blog”;
echo “”;
// Iterate through the list and create the unordered list
foreach ($feed->channel->item as $item) {
echo ‘link .’” title=”‘ . htmlspecialchars(strip_tags($item->description)) . ‘ “>’ . $item->title . ‘‘;
}
echo “”;
}
}
?>
Bonus feature here :-): Using the title attribute on the anchor tag (a href=) means that on hovering the link a preview of the feed’s description is displayed (the socalled “tooltip”).
Ronald
March 1st, 2008 at 10:03 am
mmm, code seems to be messed up, just tryig to use tags. Moderator, if it doesn’t work, feel free to remove my reply.
<?php
$values = get_post_custom_values("rss_url");
$rss_url = $values[0];
if ($rss_url) {
// Load the XML file into a Simple XML object
$feed = simplexml_load_file($rss_url);
if ($feed) {
echo "Feeds";
echo "";
// Iterate through the list and create the unordered list
foreach ($feed->channel->item as $item) {
echo 'link .’” title=”‘ . htmlspecialchars(strip_tags($item->description)) . ‘ “>’ . $item->title . ‘‘;
}
echo “”;
}
}
?>
March 1st, 2008 at 10:11 am
still a mess, sorry!
March 3rd, 2008 at 11:41 pm
The only problem with using SimpleXML is that RSS is only occasionally ever actually valid XML. If you’re parsing clean, perfect XML (such as from a web service API), SimpleXML is great. If you want to parse all of the imperfections of HTML repackaged in XML (aka RSS or Atom), you need something that is properly tuned for such a task.
March 12th, 2008 at 3:42 pm
Hello, Nice writeup about Add latest RSS items to Wordpress Post. I would have to agree with you on this one. I am going to look more into . This Wednesday I have time.
March 12th, 2008 at 6:54 pm
This tutorial is great, thanks a lot, i searched for something like this a while. Very plain and simple.
I will use this on my blog directory.
March 13th, 2008 at 10:55 am
Finding your site was an accident thanks to google, but I like it
May 14th, 2008 at 11:49 am
WordPress does have a built-in RSS parser: fetch_rss(). At least in the V2.x series
July 14th, 2008 at 12:36 pm
[…] Add latest RSS items to Wordpress Post […]