Posted on 18 January, 2010 By 14 Comments

How to Add a RSS Feed Without a Plugin Using the WordPress Fetch Feed Function

 

Fetch (by JJay)

Why back in the dark ages of this site I wrote an article about how to create a delicious bookmarks page. This is a page with a piece of javascript provided by delicious and is pulling my bookmarks that are tagged with WordPress.

Well, call me dense but there is a WordPress function called Fetch Feed that can be used to do the same thing and can actually be used to pull any RSS feed to your templates without the use of a plugin.

Sometimes I wonder why it takes me so long to notice things like this. I guess I don’t really study everything WordPress can do unless I am specifically searching for a solution.

Anyway, I was going through my feed reader, which is a rare occurrence these days, and came across this article about adding a flickr gallery to WordPress without a plugin. And I thought, “why didn’t I realize this function existed?” and “what could I use it for?”

I realized it was a perfect alternative for listing bookmarks rather than using the delicious snippet of javascript.

The code provided by the Codex as an example is the following:

<h2><?php _e('Recent news from Some-Other Blog:'); ?></h2>
<?php // Get RSS Feed(s)
include_once(ABSPATH . WPINC . '/feed.php');

// Get a SimplePie feed object from the specified feed source.
$rss = fetch_feed('http://example.com/rss/feed/goes/here');

// Figure out how many total items there are, but limit it to 5.
$maxitems = $rss->get_item_quantity(5); 

// Build an array of all the items, starting with element 0 (first element).
$rss_items = $rss->get_items(0, $maxitems);
?>

<ul>
    <?php if ($maxitems == 0) echo '<li>No items.</li>';
    else
    // Loop through each feed item and display each item as a hyperlink.
    foreach ( $rss_items as $item ) : ?>
    <li>
        <a href='<?php echo $item->get_permalink(); ?>'
        title='<?php echo 'Posted '.$item->get_date('j F Y | g:i a'); ?>'>
        <?php echo $item->get_title(); ?></a>
    </li>
    <?php endforeach; ?>
</ul>

Now I’m sure you’re all thinking, “Ok. That’s great Kim. Thanks for throwing that load of goop at us.” But, it will work fine with most templates and only two parts of it need to be changed.

  1. Change Recent news from Some-Other Blog to a title that makes sense.
    <h2><?php _e('Recent news from Some-Other Blog:'); ?></h2>
    
  2. And you put the url to your feed where it says http:http://example.com/rss/feed/goes/here
    // Get a SimplePie feed object from the specified feed source.
    $rss = fetch_feed('http://example.com/rss/feed/goes/here');
    
  3. You can also change the number of items that are pulled by the feed but that isn’t necessary. Change the 5 to the number of items that you want.
    // Figure out how many total items there are, but limit it to 5.
    $maxitems = $rss->get_item_quantity(5);
    

When I started this post I mentioned using fetch feed to pull delicious bookmarks. I got the rss feed for my bookmarks with the tag wordpress and placed it in the footer of this page. If you scroll down to the bottom you will see the result of the code in the footer. This could be used in sidebar widget, if you are also using a plugin like Exec-PHP that allows code in text widgets or you could create a custom page template to display feeds.

Can you think of interesting uses for this? So far, I’ve been thinking about a custom news page or a custom lifestream page that pulls in feed from the social networking sites that you use.

Sidenote: I used to have a cat that would fetch. If I shot a rubber band across the room, she would chase it, pick it up in her mouth and bring it back to me so I would throw it again.

Related posts:

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.




Templatic

14 Responses to “How to Add a RSS Feed Without a Plugin Using the WordPress Fetch Feed Function”

Trackbacks/Pingbacks
  1. The WordPress Fetch Feed Function and the Feed Cache | (Anti) Social Development
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

    • harm: You no longer can "Add to My Page" This used to be simple, using the facebook UI. Unfortunately facebook removed thi...
    • Phil Derksen: I was referencing plugin #1 in the list btw. :)...
    • Phil Derksen: Thanks for mentioning my Pin It button plugin. I'd love to hear what people think of it and what they'd like to see adde...
    • Jim: I started playing around with Pinterest about a week ago and while I can see why people say it's a social network for wo...
    • Jim: I learned this the hard way a few years ago when my laptop died. Nowadays I back up everything regularly to an external ...

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