Feed Me! Bring an RSS Feed Anywhere in your Site

Tutorial

Many of us are guilty of running multiple blogs – we have personal blogs, work blogs, and even blogs for our pets.  If you’re like me, however, you still like to show the different sides of yourself around your different sites.  WordPress’ built in RSS Widget does a great job at bringing external feeds into your site, but what if you want a little more control?  And what if you don’t want to constrain the feed to a widget, but give it a spot of prominence in a page template?  Well, that’s where SimplePie comes in – and the best part: it’s built into WordPress.

SimplePie is a very simple (hence the name) implementation of a php script used to scrape (fetch) a feed and display its contents.

Below is a self-made modification of the RSS sample given on the codex; this is a simple start, but it allows you to modify it like a typical WordPress loop (and structures it very similarly):

<?php if(function_exists(‘fetch_feed’)) {
include_once(ABSPATH.WPINC.’/feed.php’);
$feed = fetch_feed(‘#’); // Replace the hash mark with your feed URL
$limit = $feed->get_item_quantity(3); // specify number of items to show
$items = $feed->get_items(0, $limit); // create an array of items
}
if ($limit == 0) echo ‘<div>The feed is either empty or unavailable.</div>’; // Message to show if 0 items in feed
else foreach ($items as $item) : ?>
<div class=”post”>
<a class=”rsswidget” href=”<?php echo $item->get_permalink(); ?>”
title=”<?php echo $item->get_date(‘F j Y @ g:i a’); ?>”><h2><?php echo $item->get_title(); ?></h2></a>
<p><?php echo $item->get_date(‘F j Y’); ?></p>
<div class=”entry”>
<?php echo substr($item->get_description(), 0, 100); ?> // change 100 to number of characters to show
<a class=”fullpost” href=”<?php echo $item->get_permalink(); ?>”>read more</a>
</div>
</div>
<?php endforeach; ?>