If you are not fan of using too many plugins on your wordpress site, you can minimize the use by adding bits of code to your theme’s template files, for example, list the most recent comments, or display the most recent posts.
Doing this is very easy, the only thing you will need to do is choose a location and paste the code. Of course you can modify it to suit you.
How to list the most recent comments
#1 - Login to your wordpress dashboard
#2 - Click on Appearance and proceed with Editor
#3 - Find the most appropriate theme template to paste the code (I’m assuming the sidebar.php or footer.php)
The code to display the 5 most recent comments
<?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,50) 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 5";
$comments = $wpdb->get_results($sql);
$recentComments = $pre_HTML;
$recentComments .= "\n<ol>";
foreach ($comments as $comment) {
$recentComments .= "\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>";
}
$recentComments .= "\n</ol>";
$recentComments .= $post_HTML;
echo $recentComments ;
?>
Code explanation
This code contains 2 sections. The first section uses mysql string to get the most recent comments from the database and the second section a simple php code to output the list formatting.
By default, this code will display the 5 most recent comments. To modify this, just change the number 5 located at the end of the $sql variable.
The characters are limited to 50 per one listing, to change this locate this piece inside the code SUBSTRING(comment_content,1,50) and change the value of 50 to the one that will suit you.
Enjoyed this? Subscribe to the full RSS Feed!









April 27th, 2009 at 6:15 pm
Hi, Everything dynamic and very positively!
June 29th, 2009 at 11:32 am
Hey was looking this for quite some time. Thanks for the code.
July 1st, 2009 at 4:13 pm
Excellent help.:P
Thanks. I was searching for this help since last 2 months, finally it was hidden inside a banana peel.
keep up
July 13th, 2009 at 10:31 am
I’ve been looking for this kind of tuts for the last couple of months.. Everywhere else always suggested to use plugin…
Thanks a bunch, Voya…!
July 18th, 2009 at 1:19 pm
thnx just installing the code and uninstalling the plugin
January 11th, 2010 at 4:09 am
There is obviously a lot to know about this. There are some good points here.
I’m Out!