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.
- Change Recent news from Some-Other Blog to a title that makes sense.
<h2><?php _e('Recent news from Some-Other Blog:'); ?></h2>
- 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');
- 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.
stratosg says
Kim there is something extremely wrong with your footer…. Whatever you did needs some fixing ;)
Kim Woodbridge says
Thanks Stratos – That was weird because I’ve had the code on the site for a week before posting this article. And it’s not this code that caused the problem – it was a stupid error from last night. I commented out some code in the sidebar and used –? rather than –> Duh! At least it was only broken for 12 hours rather for a week.
Dennis Edell says
The only feed plugoin I use is for feedburner…will this replace that?
.-= Dennis Edell´s last blog ..Blog Move Is Imminent ! I’m Looking For Launch Partners… =-.
Kim Woodbridge says
Hi Dennis – No, this is a replacement for a plugin like the RSS widget that comes with WordPress.
Dennis Edell says
I thought that after posting, but didn’t wanna look stupid twice in the same comment section. lol
.-= Dennis Edell´s last blog ..Blog Move Is Imminent ! I’m Looking For Launch Partners… =-.
Dennis Edell says
Just so I have this right…check out my footer, see the two feeds? Is this what you’re referring to?
.-= Dennis Edell´s last blog ..Blog Move Is Imminent ! I’m Looking For Launch Partners… =-.
Kim Woodbridge says
Hi Dennis – It looks like you have a widgetized footer and are using the RSS feed widget, right? This code example would be used instead of the widget and could be placed anywhere within the template files.
Dennis Edell says
OK not that’s cool. I have an idea in mind and if I had to use all widgets, the blog would take 15 minutes to load. LOL
vered | blogger for hire says
You should turn this blog into an eBook at some point… will be very useful.
.-= vered | blogger for hire´s last blog ..High Heels Sexy? =-.
Kim Woodbridge says
Thanks Vered – I have lots of ideas right now but not enough time. ;-)
Norbert says
hey Kim thank you so much for getting this. Copy&pasting your snipped i noticed that the link tag isn’t closed …just to note for other guys using it.
Kim have you any idea how (in “the simplepie-loop”) i can read out the value of a customfield of an item(post)?
Thanks very much
Kim Woodbridge says
Hi Norbert – Thanks I fixed that.
That’s a good question – I don’t have an answer without testing it. Maybe someone has posted something like that on the WordPress forums.
Grant says
Hi, I’ve recently added this code to my site and for some reason keep getting the “No items returned” statement. I changed the code around slightly, here’s what I have:
FEATURED PARTNERS
get_item_quantity(3); // specify number of items
$items = $feed->get_items(0, $limit); // create an array of items
} ?>
<?php
if ($limit == 0) echo 'The feed is either empty or unavailable.';
else foreach ($items as $item) : ?>
<a href="get_permalink(); ?>"
title="get_date('j F Y @ g:i a'); ?>" target="_blank">
get_title(); ?>
Thanks!
Grant says
Fixed code…
get_item_quantity(3); // specify number of items
$items = $feed->get_items(0, $limit); // create an array of items
} ?>
<?php
if ($limit == 0) echo 'The feed is either empty or unavailable.';
else foreach ($items as $item) : ?>
<a href="get_permalink(); ?>"
title="get_date('j F Y @ g:i a'); ?>" target="_blank">
get_title(); ?>