Posted on 18 January, 2010 By Kim Woodbridge 12 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.

photo credit: JJay


Related Posts:
  • How to add a RSS Feed to a Post or Page Using a WordPress Shortcode
  • WordPress – How To Create a Delicious Bookmarks Page
  • How to Show an April Fool’s Day Message on Facebook Using FBML
  • The WordPress Fetch Feed Function and the Feed Cache
  • How to Import Your WordPress Blogroll or Links From One Site to Another
  • Posted In : WordPress Tips
    If you enjoyed this post, please subscribe to my RSS Feed

    Post to Twitter   Post to Delicious   Post to StumbleUpon

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


    Recent Comments

      • Kim Woodbridge: Hi Dean - Thanks! I'm just glad I got the punctuation right - I've been having a rough week with typos ......
      • Kim Woodbridge: Hi Jim - What's not to love about Mr T?...
      • Kim Woodbridge: Hi - Thanks for visiting! It is funny :-)...
      • Dean Dwyer: Ah the power of the comma. It's April, Fools. Who knew punctuation could be so funny. Thanks for the smile....
      • Kim Woodbridge: Hi Christine - Awesome! Ooh ... something shiny ;-)...
      • Kim Woodbridge: Hi - I wish Twitter lists had started sooner. I've worked on them some but I'm having a hard time finding the time to w...
      • Christine: This has been driving me crazy for... maybe forever! I created too many lists and deleting them was not exactly clear -...
      • Ching Ya: Honestly I can't think of a way to live without lists. :-) Twitter lists, Facebook lists.. all of these have helped me a...