Display Wordpress Feeds On Your Site With SimplePie

20th February 2008 - 2 minutes read time

You can display your latest Wordpress posts anywhere on your site by using an RSS reader called SimplePie and a few lines of code. SimplePie is a fast and efficient RSS reader, and it will also cache feeds to reduce the amount of processing time taken.

Download simple pie from the website and upload the simplepie.inc file to your web server. Next include the following section of code anywhere on your site that you want to display the latest post on.

<?php
require_once('simplepie.inc');
 
$feed = new SimplePie();
$feed->set_feed_url('http://www.example.com/feed');
 
$feed->init();
 
echo '<ul>';
 
// Limit the items to be shown
foreach($feed->get_items(0, 3) as $item){
  echo '<li><a href="'.$item->get_permalink().'">'.$item->get_title().'</a><br />'. "\n";
  echo substr($item->get_description(),0,180).'...</li>'."\n";
}
echo '</ul>';
?>

To display all items in the feed just use the following foreach declaration:

foreach($feed->get_items() as $item){
}

You can also pull out categories from your Wordpress blog by setting the feed URL to something like this.

http://www.example.co.uk/category/example/feed

This will allow you to display a page will posts from different categories.

Of course you can use this technique on any RSS generating blog platform, it doesn't have to be Wordpress, but it is much better than trying to figure out database connections and the like.

Comments

Permalink
You can simplify even further by passing the $start and $length values to get_items() in the foreach loop: foreach($feed->get_items(0,3) as $item) { ... }

Ryan Parman (Sat, 03/01/2008 - 18:53)

Permalink
why it is not work for my blog, i already follow your instruction but not work, please help me.

parama dewa (Thu, 03/05/2009 - 15:54)

Permalink
Thanks for this. What if I want latest post from a group of categories?

Ayo (Thu, 10/04/2012 - 20:26)

Permalink
Do categories have RSS feeds? It's been so long since I used WordPress but that would be my first thought...

Add new comment

The content of this field is kept private and will not be shown publicly.