Display RSS feeds on your webpage

Someone asked me this week how I display RSS feeds from three external sources on my personal website ErikBainbridge.com if it’s not a WordPress website.  WordPress after all offers a convenient function, RSSImport(), for reading RSS feeds on a WordPress webpage, but there is no equivalent function that I’m aware of for non-Wordpress webpages.

The answer is that I cheated.  Although ErikBainbridge.com doesn’t appear to be a WordPress website, it actually is.  I’m just not using a Theme and I don’t have a blog on that website.  I simply built WordPress into it so I would have access to RSSImport().

It’s a solution that works, but building a website with WordPress underlying it is a lot of work just to have easy access to RSS feeds.  Is there an easier way?

Yes, there is, and I use it on my AvatarPlanet.com website, where I display the three most recent posts from my Avatar Planet blog.  I could display RSS feeds from any website, not just my own.  It’s equally as capable as using RSSImport(), and the way I did it is actually easier to implement as long as you don’t mind dealing with a few lines of PHP.

Before I start, there are several caveats you need to be aware of.  In order for this to work, your PHP implementation needs to have PEAR and XML_Parser installed, and allow_url_fopen must be set to “ON”.  Unless you are the system administrator, you probably won’t have control over either of these.  Assuming that your system is configured this way,  you should be able to fairly easily display RSS feeds on your webpage.

The following is how I did it on avatarplanet.com to read feeds from my blog, blog.AvatarPlanet.com and to display the three most recent posts.  The output is in the form of a bulleted (unordered) list in which every line is the headline from the RSS feed that you can click to read the full post :

<?php
require_once “XML/RSS.php”;

$rss =& new XML_RSS(“http://blog.avatarplanet.com/?feed=rss”);
$rss->parse();

echo “<ul>”;
$number_posts=3;
foreach ($rss->getItems() as $item) {
   if (++$i <= $number_posts) {
      echo “<li><a href=\”” . $item[‘link’] . “\”>” . $item[‘title’] . “</a></li><br>”;
   }
}
echo “</ul>”;
?>
If you haven’t written PHP before, here are a few basics you need to know:

  1. You insert these lines into your HTML file.  In order for them to work, you’ll  need to change the filename’s extension from .html or .htm to .php.  Be sure to remove the old file from the directory, or there may be unpredictable results, especially if the file is named index.htm or index.html.
  2. You should copy the above lines of PHP and paste them into your HTML where you want it displayed.
  3. “<?php” and “?>” are opening and closing tags that mark the lines as PHP rather than HTML.
  4. You need to copy and paste these lines exactly as they appear here.

There are only two changes you need to make, other than formatting changes (for example if you don’t want it to appear as a bulleted list):

  1. Change “blog.avatarplanet.com/?feed=rss” to the url of whatever RSS feed you want.  For example, if you wanted the New York Times home page RSS feed, you would use the url “rss.nytimes.com/services/xml/rss/nyt/HomePage.xml’.
  2. This example displays the three most recent items in the RSS feed.  If you want a different number, change the number 3 in the line “$number_posts=3” to whatever number you want.

If you want to display RSS feeds from more than one url, just repeat everything in the above code except the line containing “require_once “XML/RSS.php”;”  All you have to change is the url.

I can’t guarantee this method of displaying RSS feeds will work for you, but as long as your system is configured the way I described above, it should work.

Leave a Reply

Your email address will not be published. Required fields are marked *