X

How to Fetch and Display RSS Feed in WordPress

Snippets by IsItWP

Are you looking for a way to fetch an RSS feed and display it in WordPress? While there’s probably a plugin for this, we have created a quick code snippet that you can use to fetch and display RSS feed in WordPress.

Instructions:

All you have to do is add this code to your theme’s index.php file:

<?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; ?>

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly add code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: 24 best WordPress education themes and how to secure your WordPress forms.

Comments   Leave a Reply

  1. Hello!
    The function I was using before was outdated and produced the dreaded Function create_function() is deprecated error. This is working great with all the latest Apache software. Many thanks!

  2. Hey,

    Nice but I need to display also the thumbnail, do you know how to ?
    Thank you.

  3. every time i saw this or similar pages from searching google. no one explained step by step on how to install this. Would be nice if someone would stop expecting everyone to know this and give directions

  4. Display an RSS Feed in Your WordPress Theme | ChurchMag September 9, 2011 at 9:41 am

    […] from your other blog.Drop a RSS news feed in your theme.What other uses would be awesome?[via WordPress Code Snippets] About Eric Dye: I believe in media that matters. I have produced radio for over 15 years, […]

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!