Publish RSS feeds on your WordPress site without plugins
February 13, 2008 – 12:03 pm | by Miriam SchwabDarren Hoyt, that brilliant WordPress developer and designer, explains how you can publish RSS feed headlines on your WordPress blog with a built-in WordPress function instead of using a plugin.
As Darren says, this code snippet is not intended for scrapers; it’s for people who have more than one blog, or are part of a blogging network, and would like to have their readers exposed to their other blog material.
The necessary code is what pulls all the Planet WordPress feeds into your WordPress dashboard. Darren took that code from wp-admin/index-extra.php and rewrote it into a snippet which you can insert into your theme files:
<?php
require_once (ABSPATH . WPINC . '/rss-functions.php');
// here's where to insert the feed address
$rss = @fetch_rss('http://www.darrensmusicnews.com/feed/');
if ( isset($rss->items) && 0 != count($rss->items) ) {
?>
<ul>
<?php
// here's (5) where to set the number of headlines
$rss->items = array_slice($rss->items, 0, 5);
foreach ($rss->items as $item ) {
?>
<li>
<a href='<?php echo wp_filter_kses($item['link']); ?>’>
<?php echo wp_specialchars($item['title']); ?>
</a>
</li>
<?php } ?>
</ul>
<?php } ?>
Obviously, you have to change the feed address on the fourth line to yours. You can also select how many items from the feed should appear.
In the comments, Darren says that you can publish multiple lists of feeds by copying the snippet with several different RSS feeds. If you want to aggregate a bunch of feeds into one single feed that you’ll republish on your blog, he recommends the Easy Blog Networking for WordPress Blogs plugin, which combines a bunch of feeds and republishes them in one list, or Yahoo Pipes.
Another Option from the WordPress Codex
Scot Hacker brings another variation for displaying RSS feeds on a site: the WordPress function fetch_rss. On the WordPress Codex, you can see an example for displaying a list of links for an existing RSS feed, limiting the selection to the most recent 5 items:
<h2><?php _e(‘Headlines from AP News’); ?></h2>
<?php // Get RSS Feed(s)
include_once(ABSPATH . WPINC . ‘/rss.php’);
$rss = fetch_rss(‘http://example.com/rss/feed/goes/here’);
$maxitems = 5;
$items = array_slice($rss->items, 0, $maxitems);
?>
<ul>
<?php if (empty($items)) echo ‘<li>No items</li>’;
else
foreach ( $items as $item ) : ?>
<li><a href=’<?php echo $item['link']; ?>‘
title=’<?php echo $item['title']; ?>‘>
<?php echo $item['title']; ?>
</a></li>
<?php endforeach; ?>
</ul>



9 Responses to “Publish RSS feeds on your WordPress site without plugins”
By Darren Hoyt on Feb 14, 2008 | Reply
Thanks for the kind words, Miriam. Now that I look at it, I think I like Scot’s version better
By Miriam Schwab on Feb 14, 2008 | Reply
Darren - really? Good to know!
By Javier on Mar 14, 2008 | Reply
Hi i’m trying to do this. Where do i put the script? I’m lost
thanks for any help
By Ryan on Mar 17, 2008 | Reply
Javier - I haven’t tried it, but by the looks of it you can just copy and paste the code above into the part of your theme where you want it to display and it should work automatically (you need to change the feed URL first though).
By Javier on Mar 17, 2008 | Reply
My site is hosted on wordpress.com. How do I edit my theme to include this?
By Ryan on Mar 19, 2008 | Reply
Javier - I don’t think that is possible. AFAIK the only thing you can edit in WordPress.com themes is the CSS and even that you need to pay an extra ~US$15 for.
By Ehab on Apr 11, 2008 | Reply
Hey !
Nice little snippet. I am using something different, but none the less - same.
Do you think there is a way to strip out posts with the title “Hello World” from being parsed ?
Would really appreciate