PureNews

PureNews is an amazingly sleek and powerful news theme with unlimited color variations.

View full feature list Check out the live demo Buy this theme today

Improve your Recent Posts

Posted by on 11th Jan 2009 WordPress Coding & Design 21 comments

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:

[sourcecode language="php"]< ?php
$myposts = get_posts('numberposts=5');
foreach($myposts as $post) :
?>

  • if (is_single()) $postid = $post->ID;[/sourcecode]

    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.

    [sourcecode language="php"]< ?php
    $myposts = get_posts('numberposts=5&exclude='.$postid);
    foreach($myposts as $post) :
    ?>

  • get_the_category() function, to get details of the category that the post is currently in. The code to do this is

    [sourcecode language="php"]$catchk = “”;
    if (is_single()) :
    $cat = get_the_category();
    $catID = $cat[0]->cat_ID;
    $catchk = “&category=”.$catID;
    endif;[/sourcecode]

    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

    [sourcecode language="php"]< ?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;
    ?>

    Recent Posts< ?php echo $rptitle ?>

      < ?php
      $myposts = get_posts('numberposts=5&exclude='.$postid.$catchk);
      foreach($myposts as $post) :
      ?>

  • A PHP Developer using WordPress to power both blogging and commercial CMS sites. I've written and released a couple of plugins for WordPress and am currently writing plugins for use on commercial websites.

    21 comments - Leave a reply
    • Posted by Rarst on 11th Jan 2009

      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.

    • Posted by axel on 11th Jan 2009

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

    • Posted by Alex | Blogussion.c on 11th Jan 2009

      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!

    • Posted by Sarah on 11th Jan 2009

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

    • Posted by Dennis Edell on 11th Jan 2009

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

    • Posted by Kevin Muldoon on 11th Jan 2009

      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.

    • Posted by Sarah on 11th Jan 2009

      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.

    • Posted by ilahiler dinle on 12th Jan 2009

      It is realy super…

      thanks

    • Posted by SEO Strategies on 12th Jan 2009

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

    • Posted by Dennis Edell on 12th Jan 2009

      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

    • Posted by Bernard@Chester NJ P on 12th Jan 2009

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

    • Posted by Laura-Whateverebay on 12th Jan 2009

      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.

    • Posted by Sarah on 13th Jan 2009

      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-co… which will allow you to control it a lot more and remove names if you wish.

    • Posted by Dennis Edell on 13th Jan 2009

      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.

    • Posted by Steve on 13th Jan 2009

      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

    • Posted by Sarah on 18th Jan 2009

      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.

    • Posted by Nihar on 20th Jan 2009

      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.

    • Posted by Steve Bruner on 21st Jan 2009

      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:

      <code>

      ID;

      $myposts = get_posts('numberposts=1&category_name=featured&exclude='.$postid);

      foreach($myposts as $post) :

      ?>

      <div id="post-" class="featured">

      <a href="" title=

      </code>

    • Posted by Steve Bruner on 21st Jan 2009

      whoop! Let's try that again:

      <code>

      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=

      </code>

    • Posted by Steve Bruner on 21st Jan 2009

      Wish there was a Preview button….