On 90% of my clients projects that are running WordPress I usually don’t run their Recent Comments anywhere on the site but in some rare cases they might actually want them. I was talking to my partner Greg today and he was wanting to do this on his site.
I had to stop and think what the calls were to do this since I rarely do this but I figured it might happen again someday so here is the code you can drop into your sidebar.php file on your theme to accomplish this.
<h2>Recent Comments</h2>
<?php
global $wpdb;
$sql = <span style="color: #006080">"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"</span>;
$comments = $wpdb->get_results($sql);
$output = $pre_HTML;
$output .= <span style="color: #006080">"n<ul>"</span>;
<span style="color: #0000ff">foreach</span> ($comments <span style="color: #0000ff">as</span> $comment) {
$output .= <span style="color: #006080">"n<li>"</span>.strip_tags($comment->comment_author)
.<span style="color: #006080">":"</span> . <span style="color: #006080">"<a href=""</span> . get_permalink($comment->ID) .
<span style="color: #006080">"#comment-"</span> . $comment->comment_ID . <span style="color: #006080">"" title="on "</span> .
$comment->post_title . <span style="color: #006080">"">"</span> . strip_tags($comment->com_excerpt)
.<span style="color: #006080">"</a></li>"</span>;
 
}
 
$output .= <span style="color: #006080">"n</ul>"</span>;
$output .= $post_HTML;
echo $output;?>
jamland says
Thanx. This works for me
limone says
thank you, worked well for me too! 🙂
home builder seo says
i read this site i think there are more concept for seo!
Helen D says
This code is great. The only thing I would like to do, is display some text in place of comments if none are available. ie “There are no comments to display”
I know it's a bit odd to put the code into the sidebar when there are no comments. I simply want to cover this eventuality in the sidebar so I can launch the site, and not have to worry about when or if comments are submitted to the site.
Helen D says
This is great thank you. I just wondered if there is a way to show a line of text – ie, there are no comments – when no comments exist on a new site?
I've been trying to show or remove the whole block with if / else, but I can't get it to work.
Thanks,
H
Generators for Sale says
The script works well. Thank you for this.
Aye says
Thanks, it was very helpful.
Web Hosting Service says
Recently I have been seeing these recent comments, popular posts and recent posts sections on most of the blogs I visit. I thought it was possible by some plugins, and I was correct upto some extent.
Johnny says
I’ve been attempting this for a while so glad I cam across this post. Thanks!