Post Pic

WordPress tip : Fetch and display RSS feeds

Do you know that WordPress have a built-in RSS reader? Some time ago, I published a recipe to let you know how to use it. Today, I’m glad to show you an updated version of this very popular hack.

Simply paste the following code where you want the feed to be displayed. Don't forget to define feed url at line 4.

<?php if(function_exists('fetch_feed')) {

	include_once(ABSPATH.WPINC.'/feed.php');
	$feed = fetch_feed('http://feeds.feedburner.com/catswhoblog');

	$limit = $feed->get_item_quantity(7); // specify number of items
	$items = $feed->get_items(0, $limit); // create an array of items

}
if ($limit == 0) echo '<div>The feed is either empty or unavailable.</div>';
else foreach ($items as $item) : ?>

<div>
	<a href="<?php echo $item->get_permalink(); ?>"
	  title="<?php echo $item->get_date('j F Y @ g:i a'); ?>">
		<?php echo $item->get_title(); ?>
	</a>
</div>
<div>
	<?php echo substr($item->get_description(), 0, 200); ?>
	<span>[...]</span>
</div>

<?php endforeach; ?>

Thanks to Jeff Starr from Digging into WordPress for this great tip! Have you checked out Jeff's book by the way? It's great! (Review here)

8 Responses

Nov 30 2009 15:00

Great little code snippet. This could be developed into something much more complex pretty easily, and also presented very nicely into posts as well.

Nov 30 2009 19:10

Josh, other than a plugin, which I haven’t been able to find, how would one go about generating a post from the new RSS feed?

Dec 01 2009 08:03

really informative for WP users.

Dec 01 2009 17:31

You can also pull RSS feeds into posts or pages with the very handy RSS Shortcode plugin by Yoast.

http://wordpress.org/extend/plugins/rss-shortcode/

Dec 02 2009 03:56

I’ve been looking for some code like this for one of the blogs I manage. This post was a really great find, I’m new to all of this and RSS are still a bit of a blindspot for me.

Feb 12 2010 09:17

Can I put a custom field function where the rss url goes?

example in this line.
fetch_feed(‘http://feeds.feedburner.com/catswhoblog’);

thanks.

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required