Display Wordpress Feeds On Your Site With SimplePie

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

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) { ... }
Permalink
why it is not work for my blog, i already follow your instruction but not work, please help me.
Permalink
Thanks for this. What if I want latest post from a group of categories?
Permalink
Do categories have RSS feeds? It's been so long since I used WordPress but that would be my first thought...
Name
Philip Norton
Permalink

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
7 + 1 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.