Categorized | How to, Wordpress

Show recent comments in your Wordpress Blog.

Posted on 15 April 2009 by Raman

show-recent-comments1

Displaying recent comments on your blog, lets your visitors know about the recent comments on your blog and also encourages them to make comments.

Though “Recent Comments” does not come as a default widget with Wordpress, there are many plugins available for this functions. But if you don’t want to install anymore plugins, then here is the simple code snippet for you.

<?php
global $wpdb;
$sql = “SELECT DISTINCT ID, post_title, post_password, comment_ID, comment_post_ID, comment_author, comment_date_gmt, comment_approved, comment_type,comment_author_url, SUBSTRING(comment_content,1,30) AS com_excerpt FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) WHERE comment_approved = ‘1′ AND comment_type = ” AND post_password = ” ORDER BY comment_date_gmt DESC LIMIT 10“;

$comments = $wpdb->get_results($sql);
$output = $pre_HTML;
$output .= “\n<ul>”;
foreach ($comments as $comment) {
$output .= “\n<li>”.strip_tags($comment->comment_author) .”:” . “<a href=\”" . get_permalink($comment->ID).”#comment-” . $comment->comment_ID . “\” title=\”on “.$comment->post_title . “\”>” . strip_tags($comment->com_excerpt).”</a></li>”;
}
$output .= “\n</ul>”;
$output .= $post_HTML;
echo $output;
?>

Just place this code, wherever you want to show recent comments, may be your sidebar.

This code will list the 10 most recent comments. If you want to list more or less comments, simply change the “10″ in the $sql variable highlighted in red.

Thats it you are done.

If you enjoyed this post, make sure you subscribe to my RSS feed!

Related posts

2 Comments For This Post

  1. mssmotorrd Says:

    It’s the first time I commented here and I must say you share us genuine, and quality information for bloggers! Good job.
    p.s. You have a very good template for your blog. Where did you find it?

  2. Raman Says:

    thanks….. I got the template from woo themes

Leave a Reply

Affiliate Theme Advertise Here
Advertise Here