Home > PHP > Display Wordpress Feeds On Your Site With SimplePie

Display Wordpress Feeds On Your Site With SimplePie

February 20th, 2008 Tech Leave a comment Go to comments

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
$i = 1;
foreach($feed->get_items() as $item){
 if($i<3){
  echo '<li><a href="'.$item->get_permalink().'">'.$item->get_title().'</a><br />'. "\n";
  echo substr($item->get_description(),0,180).'...</li>'."\n";
  $i++;
 }
}
echo '</ul>';
?>

You can also pull out categories 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.

Categories: PHP Tags: , , , , , ,
  1. March 1st, 2008 at 18:53 | #1

    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) { … }

  2. March 5th, 2009 at 15:54 | #2

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

  3. June 22nd, 2009 at 18:40 | #3

    Just spent half an hour looking for code to do this as the site I was working on clashed with Wordpress, so couldn’t include the WP-header. As ever Phil, you were a lifesaver.

    But you’ve left JS now, so will you actually get this? Who knows, who cares. Thanks anyway!

  4. oscar
    July 6th, 2009 at 21:56 | #4

    can i do this for 20 blogs at the same time???

  5. July 16th, 2009 at 12:52 | #5

    the same is with me. tested simplepie with a few feeds and works but not with mine :(. I’m using wordpress feeds.

  6. August 31st, 2009 at 02:59 | #6

    I really like this blog good job.

  1. No trackbacks yet.