Posted on 27 January, 2010 By Kim Woodbridge 4 Comments

How to add a RSS Feed to a Post or Page Using a WordPress Shortcode

Short stranger (by misocrazy)

Last week I wrote about how the WordPress fetch feed function could be used to display RSS feeds on a website rather than using the RSS plugin.

While I was working on the articles it occured to me that a shortcode that provided this feature would actually be a lot easier and with the shortcode the feed could easily be used in a post or a page.

I was going to make my own and then realized that someone had probably already done so and made it available. And sure enough, the piece of code I needed was in the Mastering WordPress Shortcodes article at Smashing Magazine.

When I tested the code provided by Smashing Magazine, the RSS feed displayed at the top of the post even though the shortcode was at the bottom. This article, which is French, provided a minor change to the code, which corrected the problem.

The following code goes into your Theme functions, function.php, file:

<?php
//This file is needed to be able to use the wp_rss() function.
include_once(ABSPATH.WPINC.'/rss.php');

function readRss($atts) {
    extract(shortcode_atts(array(
	"feed" => 'http://',
      "num" => '1',
    ), $atts));

      ob_start();
    wp_rss($feed, $num);

    return ob_get_clean();
}

add_shortcode('rss', 'readRss'); ?>

Once this code is in your functions file, you can easily add a RSS feed to any page or post by adding the following shortcode.

[rss feed="http://feed url here" num="5"]

All you need to do is change the URL to the feed URL that you want to use. You can also change how many feed items are displayed.

Below is an example of using the WordPress shortcode to display my bookmarks tagged with WordPress on delicious.

photo credit: misocrazy


Related Posts:
  • How to Add a RSS Feed Without a Plugin Using the WordPress Fetch Feed Function
  • 8 WordPress Shortcodes
  • How to Import Your WordPress Blogroll or Links From One Site to Another
  • How to Create Your Own WordPress Shortcode
  • How to Style the Read More Link in WordPress
  • Posted In : WordPress Tips
    If you enjoyed this post, please subscribe to my RSS Feed

    Post to Twitter   Post to Delicious   Post to StumbleUpon

    4 Responses to “How to add a RSS Feed to a Post or Page Using a WordPress Shortcode”

    Leave a Comment
    You may use: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> .


    Recent Comments

      • Regina Fried: Hi Kim, Yes, I realized I would have to update the file each time WP is upgraded (sigh). It seems like a problem that...
      • Kim Woodbridge: Hi Regina - I have a client on 2.9.2 where the problem just started too - I'm wondering if the webhost changed something...
      • Kim Woodbridge: Hi Keith - Thanks for the info - you have some interesting ideas on how to generate income from a lyrics plugin....
      • Regina Fried: Hi Kim, I'm running WP 2.9.2 on a client's site and just ran into this problem. What do you think of the idea of modi...
      • Keith from shrewdies: Now there's another one (search the WordPress Plugin Database for Happiness Today). Like Hello Dolly, Think Of Happin...
      • Kim Woodbridge: Hi Jake - Thanks for the info. I'm sure there are new themes for video games since I wrote this article a year ago :-)...
      • Kim Woodbridge: Thanks Vered! Some are better than others but it really depends on the materials I'm given to work with....
      • Jake: I found an awesome video game theme for Wordpress, its like nothing you've ever seen before, check it out: ...