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.

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” :

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.

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.

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.

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….

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…

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.

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.

Search

This site uses cookies to improve your experience, to personalize ads and to analyze traffic. It also shares information about your use of this site with social media, advertising and analytics partners. By using this site, you agree to its use of cookies. AcceptSee Details