Posted on 27 January, 2010 By 5 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




Keep Reading:
Posted In : WordPress Tips

Did you enjoy this article? I can assist you with your WordPress or Facebook project. Contact me and I will provide you with an estimate. You can also connect with me on Facebook or follow me on Twitter. If you are looking for Hosting, WordPress Theme, Newsletter or other recommendations, please view my detailed list.




5 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> .

CommentLuv badge

About

Kim Woodbridge is an accomplished Information and Technical Consultant specializing in the entire implementation of a WordPress based website including installation, theme design, upgrades, unique customizations and ongoing site maintenance.

Recent Comments

    • Property Marbella: Hi Kim, Good article with several good tips, my friend up in Sweden has clothing stores and online sales with little tr...
    • Kim Woodbridge: This post was written 2.5 years ago. At that time people wanted detailed instructions and asked me to write it. And wh...
    • Kim Woodbridge: Pretty much all themes have a page.php file. Are you using a child theme? If so, you will want to look for the page.ph...
    • Mitch: That's why the internet has evolved into a worthless piece of "junk yard". Everybody is repeating the same thing over an...
    • Meredith: This is very straightforward info, and I think it will help, but I have one question. What if I don't have a page.php fi...

Wordpress Services

  • Installation, upgrades and maintenance
  • Conversion of existing html and css templates
  • Theme and plugin recommendations
  • CSS customizations
  • Troubleshooting and tweaks for unique situations
  • Customization for individual blogging goals and needs
  • Training and advice