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.
Related posts :
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
the code is:
get_items(0, $max_items) as $item): ?>
<a href=”get_permalink(); ?>”>get_title(); ?>
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.
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
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.
After searching for the code and tweaking with rss specifications, finally I have got it. It works great. Thanks much 😉
Thank you so much for sharing this information – I was looking for just this thing to add a list of recent blog entries to some of my pages. Great job!
Nigel
Thank you so much for the excellent post!.. you save me time, brain and life! =p
what should I do with the “code”?
If you mean the JS code generated by “rss to javascript” tools, place it anywhere on any HTML page. As for the PHP code, it should be placed inside a .php file.
Hey, I’ve put the code on my site, set up the cache folder with the right permissions and everything works fine apart from the fact that the list of posts won’t update when write a new post. If i delete the file in the cache folder then it updates, but i need it to update automatically. Any help appreciated, thanks.
The cache should update automatically if it’s over 60 minutes old. If you want it to update it more frequently, use set_cache_duration() (see the SimplePie documentation on caching for details).
Yep, that’s worked cheers for the help, great tutorial. Thanks again.
White Shadow,
Thanks much for the script. Works like a charm. Now, suppose we have a limited amount or real estate we are plugging this script into. How would we control the number of characters from the blog posting that we display? Suppose the blog posting is 5 paragraphs and 1200 characters, but I only want to display a max of 100 characters on my home page….
How would we do this?
function Bgone($string,$startpostext,$endpostext) {
unset($endpos);
unset($startpos);
$p=0;
$startpostext = strtolower($startpostext);
$endpostext = strtolower($endpostext);
$startposlen = strlen($startpostext);
$endposlen = strlen($endpostext);
$string2 = strtolower($string);
if (substr_count($string2,$startpostext) != 0)
{
$count = substr_count($string,$startpostext);
while ($p < $count)
{
$p++;
$startpos[$p] = strpos($string2, $startpostext,0);
$endpos[$p] = strpos($string2, $endpostext, $startpos[$p])+$endposlen;
$string = str_replace(substr($string, $startpos[$p], $endpos[$p] –
$startpos[$p]),”,$string);
$string2 = strtolower($string);
}
}
return $string;
}
$maxchars = 100;
$get_description = substr(get_description,0,$maxchars) ;
I dont know if this is something that would work or not….. The above code shows the limits of my coding knowledge, I borrowed some of it from a script that does what I am asking for from an old PHPBB function…
You’re on the right track. I think this would work :
Also, as far as I can tell, the Bgone() function you’ve posted above isn’t actually used in the code that follows it; so what’s that about?…
Hello Sir,
I want to add multiple feeds on same page.
Can you tell me how can I do this.
Actually whenever I try to do so it says
“Fatal error: Cannot redeclare class SimplePie in http://www.muuns.com/inc/simplepie/simplepie2.inc on line 386″
Te simplest solution would be to copy the same code several times and change the feed URL. You’d also need to remove the “include ‘includes/simplepie.inc'” statement from all the copies because this file only needs to be included once and will cause the error you mention if loaded again on the same page.
White Shadow,
Thanks for looking at the max characters thing for me. In response to your question “Where did bgone come from?”, like I said, I just copied some code from another script I had for a phpbb feed and it was so not related to what we were trying to do here. Sorry for the confusion.
I will play around with your code snippet above and see if that works for what I am trying to do. I don’t have much to offer in return for the script just yet, but I will add you to my blog roll just the same as a way of saying thanks.
You’re welcome 🙂
How to edit the code to show a link to the writer profile and a link to the category of the post?
Example: “Posted by USER on Wed, Jun 10th 2009, 18:01 to CATEGORY”