SarahImprove your Recent Posts

Written by Sarah from Stuff By Sarah

A lot of themes these days comes with the Recent Posts code added already. It’s a basic list, usually just set to display the last 5 or 10 posts. A nice list but are you making the most of it?

Remove the current Post

A recent posts list comes into its own when the user is on a single post page. It allows them to see the most recent posts you’ve written on the site. However, if the page they’re on happens to be on that list then you’ve wasted a slot on the list. So why not just remove it? If we take the (usually) default code to list recent posts of:

< ?php
$myposts = get_posts('numberposts=5');
foreach($myposts as $post) :
?>
	 <li><a href="<?php the_permalink(); ?>">< ?php the_title(); ?></a></li>
< ?php endforeach; ?>

What we need to do is just use the exclude parameter and give it the post ID, however we only want this if the user is on a single post page. To do this we use a simple piece of code which is

$postid = 0;
if (is_single()) $postid = $post->ID;

This just sets the $postid variable to be zero at first, then if we’re on a single page, change it to be the current post ID. We then feed this to the get_posts() function ie.

< ?php
$myposts = get_posts('numberposts=5&exclude='.$postid);
foreach($myposts as $post) :
?>
	 <li><a href="<?php the_permalink(); ?>">< ?php the_title(); ?></a></li>
< ?php endforeach; ?>

Recent Posts from current Category

The above code displays the recent posts from all categories. A friend of mine recently said how he wanted to restrict the posts to displaying just the last 5 posts from the category of the post that the user is currently viewing. For example, if you had a category called ASP and another called PHP, if someone views a post on PHP they’d probably be more interested in the last 5 posts from the PHP category than from both, well I know I would ;)

To do this we need to work out the category ID of the current post, and then restrict the recent posts to that category. We use the get_the_category() function, to get details of the category that the post is currently in. The code to do this is

$catchk = "";
if (is_single()) :
	$cat = get_the_category();
	$catID = $cat[0]->cat_ID;
	$catchk = "&category=".$catID;
endif;

Line 1 just sets the variable to empty so that we don’t get any PHP warnings. Then we check if we’re on a single post. If so, we get the category details, and then we get the category ID of the first category (note, for multiple categories, this code will only take the first category from the list). Then we change the $catchk variable to include the category parameter for the get_posts() function. We then add this into our get_posts function too ie.

$myposts = get_posts('numberposts=5&exclude='.$postid.$catchk);

Of course, if we’re restricting our recent posts to a specific category then it may be a good idea to add the category name into the recent posts title. So we can get the name when we’re getting the ID of the category, and simply add this into the title.

So our final code would be

< ?php
$postid = 0;
$catchk = $rptitle = "";
if (is_single()) :
	$postid = $post->ID;
	$cat = get_the_category();
	$catID = $cat[0]->cat_ID;
	$rptitle = " from ".$cat[0]->cat_name;
	$catchk = "&category=".$catID;
endif;
?>

 <h3>Recent Posts< ?php echo $rptitle ?></h3>
 <ul>

< ?php
$myposts = get_posts('numberposts=5&exclude='.$postid.$catchk);
foreach($myposts as $post) :
?>
	 <li><a href="<?php the_permalink(); ?>">< ?php the_title(); ?></a></li>
< ?php endforeach; ?>

 </ul>

These are a couple of simple changes that could potentially (depending on the nature of your site) make an improvement to your visitor’s experience.

Follow this blogger on Twitter!

Sarah Written by Sarah from Stuff By Sarah
Posted on January 11th, 2009 and filed under WordPress Coding & Design
Do not forget to subscribe to our RSS feed for updates
  • Digg This Post
  • Tweet This Post
  • Stumble This Post
  • Submit This Post To Delicious
  • Submit This Post To Reddit
  • Submit This Post To Mixx
  • BloggingTips Uses Aweber

21 Responses to “Improve your Recent Posts”

Author comments are in a darker gray color for you to easily identify the posts author in the comments

  1. Rarst says:

    Excluding current post is excellent tweak, thanks! :)

    Another good tweak is hiding recent posts on home page – it has those recent posts in already and sidebar is better spent on other things.

  2. axel says:

    Nice post! I will agree for Rarst about the recent posts on home page.

  3. I always thought it was kind of useless for the current post to be included in the Recent posts. The plugin I use should have an option for it, but it doesn’t. I’m going to go through this code now and work it in on my blog. Thanks a ton!

  4. Sarah says:

    Cheers for the comments all. I’ve debated over whether to remove Recent Posts from the front page (easily done with using the is_home() or is_front_page() conditional). However, as a visitor to other sites, I’m more likely to look at the list at the top of the sidebar of recent posts, than scroll through 5-10 posts, be them excerpts or full posts. Due to that thought, I’ve left my recent posts on my front page. Plus I have the comment count after each which I think just gives a nice quick reference for visitors without having to scroll down as I opt for 5 full posts on the front page, and with long posts, I doubt people scroll to the bottom (although I do try to use the more tag on long posts!)

    However, as always, it’s whatever you’re comfortable with and whatever suits your design :)

  5. Dennis Edell says:

    Perhaps a post on how to remove the blog owners name from recent comments?

  6. Very nice mod Sarah. I think I will remove the current post from the listing but leave them all on the home page.

    Dennis – I’m sure a few recent comment plugins allow you to do this in the plugin settings. Not sure if you have hard coded it though.

  7. Sarah says:

    Hi Dennis,

    Recent comments are usually managed by a plugin so there would be a different solution for each plugin. However, I know for my own recent comments plugin I change the template under the settings to control what is displayed, similar to what Kevin suggested.

  8. It is realy super…
    thanks

  9. Good post, Now I am thinking to follow your ideas for my blog.

  10. Dennis Edell says:

    OK it appears the only edits I can make within my plugin are Title and Comment Count, not names….any suggestions?

    I’m not adverse to switching plugins (as long as it stays compatible with CommentLuv) but I would really rather not hack the core code…..if this is necessary, please be as explicit, yet simple as possible. LOL

  11. What a nice post! Thank you so much for the advice and ideas that I found here that I might use.

  12. This is great tutorial, I just changed my theme and need to fix some bugs lol….. This is great information, I can use for my other blogs.

  13. Sarah says:

    Dennis, changing your plugin is the easiest solution. I don’t know what is compatible with Comment Luv however the plugin I use is at http://blog.jodies.de/archiv/2004/11/13/recent-comments/ which will allow you to control it a lot more and remove names if you wish.

  14. Dennis Edell says:

    Thanks much Sarah, I’ve downloaded it and will send the files to Andy (commentluv creator) for verification. I’ll let you know how it goes.

    It does say compatible up to 2.3, but I trust you have already installed it on a higher blog.

  15. Steve says:

    Sarah,

    Everytime I have an issue I Google it, and your website comes up!

    This is a great article. I’d like to add a tweak.

    I have a category called “Featured” that allows me to feature and article on the front page… essentially a sticky. When you go to a Single post, I show the feature article below the Single post to encourage clicking.

    Here’s the question: I don’t want to show the featured article under the single post when the single post IS the featured article.

    Thank you !

    Steve

  16. Sarah says:

    Hi Steve, what you’ll need to do is check the category ID of the single post against the ID of your featured category. If they match then don’t display the featured article details.

    The code you need is above but let me know if you’re not sure how to approach it.

  17. Nihar says:

    Sarah, Thank you very much for this wonderful tip. I will implement this. I noticed this feature when i was reading rarst blog. I found this link from him.

  18. Steve Bruner says:

    Sarah,

    Thank you for your help. With a few edits, I was able to create a great featured area. Here’s the code if anyone else needs it:


    ID;
    $myposts = get_posts('numberposts=1&category_name=featured&exclude='.$postid);
    foreach($myposts as $post) :
    ?>
    <div id="post-" class="featured">
    <a href="" title=

  19. Steve Bruner says:

    whoop! Let’s try that again:


    ID;
    $myposts = get_posts('numberposts=1&category_name=featured&exclude='.$postid);
    foreach($myposts as $post) :
    ?>
    Featured Article:
    <div id="post-" class="featured">
    <a href="" title=

  20. Steve Bruner says:

    Wish there was a Preview button….

Trackbacks

  1. [...] the full post at Improve your Recent Posts on [...]

Comments are closed.

Comments are closed since this post is older than 30 days. However, you can continue this discussion in our popular Blogging Forums

Subscribe To BloggingTips Via RSS Subscribe To Blogging Tips Via Email Follow Us On Twitter Follow us on Facebook Find Out More About Our Newsletter

Sponsors

Blogging Tips Newsletter

Webmaster Corner

 

Our Free E-Books

Site Partners