Advertisement
Plugins

Add the WordPress RSS Feed Anywhere In Your Theme

by

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

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!

Related Posts
  • Code
    Theme Development
    Creating a WordPress Theme from Static HTML: Creating an Archive TemplateCreating wordpress theme from html 400
    If you've been working your way through this series, you now have a functioning theme with two page templates. The steps I've demonstrated to this point are: preparing your markup for WordPress converting your HTML to PHP and splitting your file into template files editing the stylesheet and uploading your theme to WordPress adding a loop to your index file adding meta tags, the wp_head hook and the site title and description to your header file adding a navigation menu adding widget areas to the header and sidebar adding widget areas, a colophon and the wp_footer hook to the footer file creating template files for static pages. Read More…
  • Code
    Theme Development
    Creating a WordPress Theme from Static HTML - Creating a Page TemplateCreating wordpress theme from html 400
    So far in this series, I've shown you how to create a fully functioning WordPress theme from static HTML. We've covered the following steps: preparing your markup for WordPress converting your HTML to PHP and splitting your file into template files editing the stylesheet and uploading your theme to WordPress adding a loop to your index file adding meta tags, the wp_head hook and the site title and description to your header file adding a navigation menu adding widget areas to the header and sidebar adding widget areas, a colophon and the wp_footer hook to the footer file. At the moment, your theme only has one template file for displaying content—the index.php file. A powerful feature of WordPress is the ability to use template files for different kinds of content.Read More…
  • Code
    Theme Development
    Creating a WordPress Theme From Static HTML: Setting Up the HeaderCreating wordpress theme from html 400
    So far in this series, you've learned how to create a basic theme from static HTML. You've done the following: Prepared your markup for WordPress Converted your HTML to PHP and split your file into template files Edited the stylesheet and uploaded your theme to WordPress Added a loop to your index file In this tutorial, you'll work on the header.php file that you created in Part 2.Read More…
  • Code
    Theme Development
    Creating a WordPress Theme From Static HTML: Adding a LoopCreating wordpress theme from html 400
    In the first three parts of this series, you learned how to prepare static HTML for WordPress and to create a theme by splitting your HTML file into a set of template files and editing the stylesheet. You then uploaded your theme to WordPress and activated it. The theme still isn't displaying any content you add via the WordPress admin however; to do that you need to add a loop to your template files.Read More…
  • Code
    Theme Development
    Creating a WordPress Theme From Static HTML: Creating Template FilesCreating wordpress theme from html 400
    In the first part of this series, I showed you how to prepare your HTML and CSS files for WordPress, ensuring the structure would work, the code was valid and that the correct classes were being used. In this tutorial you'll learn how to take your index.html file and split it up into a set of template files for use by WordPress.Read More…
  • Code
    Creative Coding
    Build a Responsive Email Newsletter Template With RSS in WordPressThumb
    If you want to learn how to create an email newsletter template using WordPress you came to the right place. Before we get started let's make sure you fully understand what I'm trying to teach. The end results will give you a WordPress page template that is designed to be used as an email newsletter template by importing your URL with email newsletter software such as GroupMail that is responsive to email clients such as Gmail or Outlook on any device such as mobile or tablet so it will display nicely. But wait there is more – this template displays the most recent posts from any RSS feed so the email newsletter is already finished, just waiting to be sent. That explanation is a lot to take in, so it might be best if you look at the live example.Read More…