1. Code
  2. Plugins

Add the WordPress RSS Feed Anywhere In Your Theme

Scroll to top
Read Time: 4 min

If you're looking for a way to display RSS feeds anywhere on your site and you're using WordPress then you'll be happy to hear about this trick. You know about the WordPress RSS widget... but what if displaying a feed in the sidebar is not enough. This tutorial will show you how to take an RSS feed and put it anywhere in your theme, including a page or post template. This is a great solution for you folks out there using RSS feed generators (say goodbye to small footer credits) or plugins (say hello to a faster website)!

The Code

Let's start this by dishing out the code, then we'll break it down step by step. This is the code that you'll be placing into your theme template where you'd like the feed to show up. For instance, you could place this into a custom page template for a dedicated, styled RSS feed ;)

Part 1

<?php if(function_exists('fetch_feed')) {
	include_once(ABSPATH . WPINC . '/feed.php'); // the file to rss feed generator
	$feed = fetch_feed(''); // specify the rss feed
	$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) : ?>
// The actual output
<h1><a href="<?php echo $item->get_permalink(); ?>" alt="<?php echo $item->get_title(); ?>"><?php echo $item->get_title(); ?></a></h1>
<p><?php echo $item->get_date('j F Y @ g:i a'); ?></p>
<p><?php echo substr($item->get_description(), 0, 200); ?> ...</p>
<?php endforeach; ?>

The Breakdown: Part 1

The idea is to first find the feed generator include_once(ABSPATH . WPINC . '/feed.php'); which is needed to be in the correct location in order for this code to work.

Then you add your rss feed $feed = fetch_feed(''); that you want to display on your site. Some feeds require the / character at the end. Keep in mind not all rss feeds will work; if that is the case you can always turn the feed into a feedburner RSS feed.

$limit = $feed->get_item_quantity(7); will determine how many posts you'd like to show. Replace 7 with whatever number fits your needs.

If there is no posts available then it will display a error message "The feed is either empty or unavailable." Replace the text with whatever wording is appropriate. If you get this message then your code is most likely working.

    Here are your options you can display from the feed. You'll notice thumbnails are not available. Whatever you decide will be repeated if you have multiple feed posts.

  • get_title(); ?> = Post Title
  • get_date('j F Y @ g:i a'); ?> = Date, if you want to display the date a different way go here
  • get_permalink(); ?> = Link to Story
  • get_description(), 0, 200); ?> = Excerpt or Description, change 200 to whatever amount of characters you would like to display

Part 2

The default for your RSS feed refreshes with new posts every 12 hrs. This code will cause the feed to be checked every 30 mins (place this code in your functions.php or custom_functions.php theme file):

add_filter( 'wp_feed_cache_transient_lifetime', create_function('$rssfix', 'return 1800;') );

The Breakdown: Part 2

add_filter is a WordPress function call that allows you to 'hook' into the WordPress core and execute a function during a certain operation.

The hook is wp_feed_cache_transient_lifetime. It's the hook that handles the feed refreshes.

Then comes create_function('$rssfix', 'return 1800;') which sets a time to check the feed more promptly. The code is set to 30 minutes, so if you'd like a different time then change the 1800 to your desirable time. 600 = 10mins, 1200 = 20mins, 1800 = 30mins. $rssfix can be changed to whatever text you'd like, but remember to keep the $.

There you have it. Take this idea and put it in a php widget, theme, post or page template. As I mentioned at the top of the post, you could place this into a custom page template for a dedicated, styled RSS feed.

Editors Note! This article originally ended in a few final tips... a few final tips that suggested that you open up a file inside the WordPress core and make changes. These changes were beneficial overall and very well intentioned on the surface, but it needs to be said that the WordPress core code is sacrosanct, and should never, everever be changed. Can you do it? Technically, yes... but we've officially decided that this site won't go near the topic with a ten foot pole as it represents an undermining of some of the principles that makes WordPress great (a secure core, safe updates, oh, and not needing to ever touch the core to get what you want). Thanks for all of the comments - they help make the site better and they're all appreciated!

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.