Displaying Recent Posts On a Non-WordPress Page

Listing recent posts on a WordPress page is easy - there are various widgets and theme functions available just for that purpose. But what about a non-WP page, or even a different site? That’s not that hard either - simply grab some posts from the blog’s RSS feed and output them on your page. In this post I’ll explain how you can do that either with JavaScript or PHP.

JavaScript - The Easy Way

If you just want a simple box containing the most recent entries, you’re in luck - there won’t even be any programming involved. You can use one of the many free JavaScript scripts that can display RSS feeds in a variety of formats. Personally, I recommend Feed2JS. It’s straightforward and minimalistic, yet still fairly configurable. And if that doesn’t suite you, there are many alternatives available - just google “rss to javascript”.

JavaScript RSS widgets have the advantage of simplicity and minimal server performance impact (the feed is loaded client-side). However, if you want to do something more advanced, like filtering posts by author, a ready-made script might not be enough. Also, the output generated by JavaScript isn’t part of the page HTML - something to consider if you’re concerned about SEO.

PHP - The Slightly Advanced

You can also use a PHP script to fetch, parse and display recent posts. There are lots of ways to do this, but I’ll focus on one of the easiest approaches. We will use the SimplePie RSS parser to handle all the boring parts - loading the feed, parsing it, caching, etc.

Lets get started. First, Download SimplePie. You’ll get a .zip archive with a number of files, but for this you’ll only need one of them - simplepie.inc. Put this file somewhere your PHP script(s) can access it. For example, if you want to display your recent posts on http://example.com/stuff/page.php, you could put simplepie.inc in the stuff directory. Also, create a cache subdirectory in the same folder - SimplePie will automatically use it for caching the RSS feed.

Next, put this script somewhere on your page (change $feed_url to match your blog’s feed) :

<?php
$feed_url = 'http://w-shadow.com/feed/';
$max_items = 5;
 
//Load SimplePie
include 'includes/simplepie.inc';
 
//Fetch the RSS feed
$feed = new SimplePie($feed_url);
 
//Check for errors
if ($feed->error())
echo 'Error : ',$feed->error();
 
//Output up to $max_items posts 
foreach ($feed->get_items(0, $max_items) as $item): ?>
<div class="item">
<h3 class="title"><a href="<?php echo $item->get_permalink(); ?>"><?php echo $item->get_title(); ?></a></h3>
<?php echo $item->get_description(); ?>
<p><small>
Posted  <?php if ($author = $item->get_author()){ echo ' by '.$author->get_name(); }?> 
on <?php echo $item->get_date('j F Y | g:i a'); ?>
</small></p>
</div>
<?php 
endforeach; 
?>

The above script will load the specified RSS feed and display up to $max_items most recent posts. You can tweak the part between foreach and endforeach to format the output to your liking.

Additional Tips

To show recent posts from a single category use a category-specific feed URL instead of the blog’s main RSS feed. Usually you can get this URL by appending “/feed” to your category URL. For example, http://example.com/category/stuff/ => http://example.com/category/stuff/feed. More information is available in the WordPress Codex.

If you want to filter the feed to only show posts from a certain author or similar, you can check each item inside the foreach loop and skip those that you don’t want to show. For example, to only show posts from the author “James” :

if ($author = $feed->get_author()){
if ( $author->get_name() != 'James') continue; //skip if not James
} else {
continue; //no author specified, skip.

It’s also possible to aggregate multiple feeds with SimplePie - just replace the feed URL with an array of URLs and the parser will handle the rest. Example :

$feed_url = array(
'http://domain.com/feed.rss', 
'http://anothersite.org/blog/feed/',
'http://www.example.com/rss'
);

This way you can display recent posts from several blogs just as easily as from a single blog.

And that’s it. Good luck :) Feel free to drop a comment if you have any questions.

Share :
  • Digg
  • del.icio.us
  • Reddit
  • StumbleUpon
  • Technorati
  • Slashdot
  • Sphinn
  • TwitThis
Related posts :

6 Responses to “Displaying Recent Posts On a Non-WordPress Page”

  1. 1
    Antonio says:

    Hello!

    thanks for the code !!!

    I am trying to put my feed on the footer with this code

    error())
    echo ‘Error : ‘,$feed->error();

    //Output up to $max_items posts
    foreach ($feed->get_items(0, $max_items) as $item): ?>

    <a href=”get_permalink(); ?>”>get_title(); ?>

    and I get this error…. do you know how to solve it????

    Warning: ./cache/8e98e367ef3e3a92241180489d4ec42e.spc is not writeable in /home/comparat/public_html/depositosbancarios/wp-content/themes/UBDMoneymakerTheme/simplepie.inc on line 1773

  2. 2
    Antonio says:

    the code is:

    get_items(0, $max_items) as $item): ?>

    <a href=”get_permalink(); ?>”>get_title(); ?>

  3. 3
    White Shadow says:

    Did you create a “cache” subdirectory in the same directory? If yes, make sure file permissions on the cache directory are set to allow writing files from PHP. Depending on your server configuration, this could mean 755 or even 777 file permissions.

    It’s possible to disable caching, but the code will become a bit more complex in that case.

    BTW, you should enclose code in <pre> tags when posting it here.

  4. 4
    Antonio says:

    Yes, I gave 755 and now I have the following

    Fatal error: Cannot redeclare class SimplePie in /home/…../simplepie.inc on line 386

    Before, I got the “Warning” but links appeared. Now I get nothing but the error

  5. 5
    White Shadow says:

    I’d guess the PHP file containing your code gets loaded more than once per pageview. Perhaps changing the first “include” to “include_once” would help. Otherwise, you probably did modify something else; setting file permissions shouldn’t cause this kind of problem.

  6. 6
    zerohacks says:

    After searching for the code and tweaking with rss specifications, finally I have got it. It works great. Thanks much ;)

Leave a Reply