When new visitors come to your site via an internal page they won’t see the most recent posts you’ve written without having to go to the front page. If there’s nothing to entice them further into the site then they’ll leave just as quick as they came. So you want to put your latest posts in their face pretty much!
In WordPress this is easily done using the get_posts() template tag. The basic usage of this tag is
< ?php
$myposts = get_posts();
foreach ($myposts as $post) :
echo "<li><a href='".the_permalink()."'>".the_title()."</a>\n";
endforeach;
?>
This would then print out 5 list items of your recent posts, the newest one first.
The more popular parameters for get_posts() are:
- numberposts
- Specifies the number of posts to list, default is 5.
- offset
- Allows you to offset the first post to list from the first post returned e.g. offset=1 would not display the latest post but the next X after. Default is 0.
- category
- The ID of the category you only want to show posts from. Default is all.
- orderby
- Controls the order of the list, default is post_title. Can accept any value from the wp_posts table. 1
- order
- This controls the order of the posts before the list is created (then the first X are used for the list), values are ASC or DESC 2
- include
- Accepts a comma separated list of post IDs to possibly include in the list.
- exclude
- Accepts a comma separated list of post IDs to possibly exclude in the list
1 The default for this is stated as post_title on the WordPress codex, however going by experience I’ve found this to actually be the post_date.
2 The default for this is stated as ASC on the WordPress codex, however going by experience I’ve found this to actually be DESC.
Whether these differences are due to the parameters used or not I’m not sure, however to be certain it’s best to use the parameters to specify what you require and not to rely on the defaults, especially as these could be changed in the future.
Display the 5 recent posts in your sidebar
So the best option to display your 5 most recent posts in either your sidebar or header, to give yourself maximum exposure to visitors, is to use the following code
< ?php
echo "<ul>\n";
$myposts = get_posts('numberposts=5&orderby=post_date&order=DESC');
foreach ($myposts as $post) :
echo " <li><a href='".the_permalink()."'>".the_title()."</a></li>\n";
endforeach;
echo "\n";
?>
This will then output an unordered list showing the last 5 posts published (note post_date will go by the publish date, ID would go by the post ID and therefore a newer post published before an already created draft would not be in the correct position on the list).
Advanced Recent Post List
There is a lot that can be done using this template tag, more than I will go into here but I will refer back to this in the future. However a nice little addition to the recent posts is to display the comment count after each one. To access the comment count we need to run an internal function to set up the rest of the post’s data - setup_postdata(). With this function run within the foreach loop we can access all of the post data in the same way as within the WordPress loop. However for now we’re after the comment count, so the code for this is as follows:
< ?php
echo "<ul>\n";
$myposts = get_posts('numberposts=5&orderby=post_date&orderby=DESC');
foreach($myposts as $post) :
setup_postdata($post);
echo " <li><a href='".the_permalink()."'>".the_title()."</a> ".comments_number('', '(1)', '(%)')."</li>\n";
endforeach;
echo "\n";
?>
This would then display the comment count after each post title if there are 1 or more comments e.g.
- Pull Quotes for WordPress
- Warning: You Might Be Doing This Mistake (3)
- The Power Of The Mighty Pen (6)
- Tagging = Social Bookmarking Success
- Improve Bounce Rate By Optimizing 404 Pages (16)
(Note: This is just an example list and not dynamic!)
So as you can see you can offer your internal page visitors a lot more fresh information about your site regardless of where they enter it, and hopefully turn a one time visitor into a long term visitor or even subscriber. A lot more theme designers are building this into their templates however if you don’t already have it showing then I highly recommend it.







R.J. | May 4th, 2008 at 12:03 pm #
I currently already had this in my theme… maybe I need to make it more prominent. I also use the related posts plugin, which shows your your set number of related posts at the bottom of every post… It can help to grab people’s attention to read other interesting posts that are similar.
Sarah (Post Author) | May 4th, 2008 at 12:13 pm #
Hi RJ, yes I reckon it should be on of the most prominent things on your site, that and the categories list, but recent posts should be the top list that people see, it’s what I put at the top of my sidebar, after all it changes every time you make a new post, if you have the comment count there people can see the recent posts with more newer activity and conversation on them. Also if someone subscribes to your site and maybe comes to comment on one post they can spot other posts have comments that maybe they didn’t know about and perhaps go into those posts and comment on them too, so it’s good for new and current visitors.
The related posts plugin is a good option to have especially for deep linking back to older posts, same goes for using the popularity contest plugin to show your most popular posts, or the most commented posts. But your most recent posts are your most recent work so are worth a valued spot on your site
Scott Fillmer | May 4th, 2008 at 1:36 pm #
Nice suggestion, thanks for actually putting the code into the post. The one I REALLY liked is the one you are using, related posts, to pull someone deeper into my blog, but I never could get that plugin to activate in the new WP 2.5.
I haven’t even been able to figure out why it wouldn’t activate, but I have 5 running active blogs on WP2.5, all on different domains and non will activate, oh well. I do like the idea of putting the posts in a high visible area.
Sarah (Post Author) | May 4th, 2008 at 3:23 pm #
Hi Scott, which related posts plugin are you using? (Should be able to get the link via the plugins page). I’m not sure which one is in use on Blogging Tips but I can direct Kevin to this post and he can perhaps inform us
Tariq Bamadhaj | May 5th, 2008 at 8:09 am #
Hi Sarah
Thanks for the post. It’s what I have been looking for a while now. Quick question:
Is it possible to add more than one category ID to be included in a recent post list? I tried to add more than 1 using commas (,) but that did not seem to do the trick. Is there any other way around this?
Thanks
Sarah (Post Author) | May 5th, 2008 at 8:28 am #
Hi Tariq, unfortunately you can only add one category ID. To get around this I’d suggest to create a new category and set the get_posts() tag to only display posts in that category, then you can just select this category to add posts to the list. To prevent the new category from displaying in your category list use the exclude parameter in your wp_list_categories() tag too.
Hope that does the trick for you
BioTecK | May 5th, 2008 at 9:57 am #
Great tip! Made me realise that I didn’t had the “recent post” in my sidebar and so I’ve “switched” it on!!
Yan | May 5th, 2008 at 12:34 pm #
Pardon me if I ask, I am using
to display the latest posts on my sidebar.How mine is different from the one you are suggesting? Thanks.
Sarah (Post Author) | May 5th, 2008 at 3:51 pm #
Hi Yan, I’m not quite sure what you meant to write in your comment as all I can see in your comment (via the source code) is <code></code>. Looking on your site you have an unordered list for your recent posts.
So not sure what you’re using but to cover the possibilities, there are 3 ways to list your most recent posts (well 3 obvious ways)
1. Use get_posts() as explained above - most suitable method.
2. Use a plugin - was required on older versions I believe, and would possibly give more control/options, however if the WP database is altered then the plugin could fail.
3. Use a direct database query - a bit like a plugin but without activating a plugin - downside to this is if the WP database is altered then the code could fail.
Kevin | May 7th, 2008 at 7:45 am #
Sarah - Great post.
Scott - For related posts I am currently using this related posts plugin however when the new design is launched within a day or so I will be using the similar posts plugin. Feel fre to email me if you’re having any problems with it:)