Manually Add Recent Comments to Sidebar (WordPress)

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">&quot;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&quot;</span>;
$comments = $wpdb->get_results($sql);
$output = $pre_HTML;
$output .= <span style="color: #006080">&quot;\n&lt;ul&gt;&quot;</span>;
<span style="color: #0000ff">foreach</span> ($comments <span style="color: #0000ff">as</span> $comment) {
$output .= <span style="color: #006080">&quot;\n&lt;li&gt;&quot;</span>.strip_tags($comment-&gt;comment_author)
.<span style="color: #006080">&quot;:&quot;</span> . <span style="color: #006080">&quot;&lt;a href=\&quot;&quot;</span> . get_permalink($comment-&gt;ID) .
<span style="color: #006080">&quot;#comment-&quot;</span> . $comment-&gt;comment_ID . <span style="color: #006080">&quot;\&quot; title=\&quot;on &quot;</span> .
$comment-&gt;post_title . <span style="color: #006080">&quot;\&quot;&gt;&quot;</span> . strip_tags($comment-&gt;com_excerpt)
.<span style="color: #006080">&quot;&lt;/a&gt;&lt;/li&gt;&quot;</span>;
&#160;
}
&#160;
$output .= <span style="color: #006080">&quot;\n&lt;/ul&gt;&quot;</span>;
$output .= $post_HTML;
echo $output;?>

Google+

Comments

comments

  • jamland

    Thanx. This works for me

  • http://caipi.limone.de/ limone

    thank you, worked well for me too! :-)

  • salmansafder

    Blogs are always a main source of getting accurate information and provide you the handy results; you can get instant and reliable information which surely helps you in any field of your concern. I am post graduate in IT and HR. These days I am doing preparation of different online certifications and I found mcsa guide is the best helping source which is providing 100% authentic material. I also spend my extra time in surfing internet, listening music and playing games. After my exams I would like to join your group.

    Regards
    Marry Davidson

  • marrydavidson34

    Blogs are always a main source of getting accurate information and provide you the handy results; you can get instant and reliable information which surely helps you in any field of your concern. I am post graduate in IT and HR. These days I am doing preparation of different online certifications and I found mcsa guide is the best helping source which is providing 100% authentic material. I also spend my extra time in surfing internet, listening music and playing games. After my exams I would like to join your group.

    Regards
    Marry Davidson

  • home builder seo

    i read this site i think there are more concept for seo!

  • Helen D

    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

    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

  • http://portablegeneratorsforsale.net/ Generators for Sale

    The script works well. Thank you for this.

  • Aye

    Thanks, it was very helpful.

  • Pingback: Mutantfrog Travelogue » Blog Archive » Recent comments sort of back

  • Anonymous

    Recently I have seen this recent comments column on many blogs and was just wondering how this was possible, I was just thinking that this was because of a module and thought that it is possible only by enabling that module, but now I have got a alternative for that. Thanks for sharing this information…
    Register Web Domain

  • http://www.web-hosting-service.in Web Hosting Service

    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

    I’ve been attempting this for a while so glad I cam across this post. Thanks!