Add the WordPress RSS Feed Anywhere In Your Theme

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

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('http://www.brettthompsonracing.com/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):

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!

Tags:

Comments

Related Articles