solostream

Publish RSS feeds on your WordPress site without plugins

| February 13, 2008 | 14 Comments

Darren 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->items0$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>

0saves
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Tags: ,

Category: Tips

Elegant WP Themes

Comments (14)

Trackback URL | Comments RSS Feed

  1. Darren Hoyt says:

    Thanks for the kind words, Miriam. Now that I look at it, I think I like Scot’s version better ;)

  2. Miriam Schwab says:

    Darren – really? Good to know!

  3. [...] site umbrella. To aggregate the posts onto the main mage, either use the sitewide feed plugin  and parse it or any one of the most recent posts [...]

  4. Javier says:

    Hi i’m trying to do this. Where do i put the script? I’m lost

    thanks for any help

  5. Ryan says:

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

  6. Javier says:

    My site is hosted on wordpress.com. How do I edit my theme to include this?

  7. Ryan says:

    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.

  8. Ehab says:

    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 :)

  9. [...] Publish RSS feeds on your WordPress site without plugins | WordPressGarage.com [...]

  10. [...] did find a code snippet that could show the rss feed of a certain site as what you see in my sidebar where I link to my [...]

  11. leo says:

    How to show the description or the first 20 words of the post.

  12. [...] Publish RSS feeds on your WordPress site without plugins [...]

  13. cep says:

    Very useful information, thanks

  14. Bryan Barker says:

    How about the entire post, vs just the headlines?

Leave a Reply




If you want a picture to show with your comment, go get a Gravatar.

More in Tips (29 of 81 articles)