Cotton Rohrscheib

The Cotton Club Blog & Podcast

  • Home
  • Bio
    • Resume
  • Blog
    • Faith & Family
    • Marketing & Tech
    • Farm & Business
    • Entertainment
    • Health & Wellness
    • Urban Farming
    • Weekend Projects
  • Media
    • Newsletter
    • Photo Galleries
    • Instagram Feed
    • Video Archives
    • Podcasts
    • Music Playlists
  • Books
  • Connect
    • Rohrscheib Capital
    • Disclaimer
    • Privacy Policy
You are here: Home / Marketing & Tech / Manually Add Recent Comments to Sidebar (Wordpress)

Manually Add Recent Comments to Sidebar (WordPress)

August 28, 2009 by Cotton Rohrscheib 9 Comments

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;?>

Share this post on:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pinterest (Opens in new window) Pinterest

Related

About Cotton Rohrscheib

The Cotton Club is a monthly podcast hosted by me, Cotton Rohrscheib. I'm a 52 year old entrepreneur w/ ADHD, OCD (and now AARP) that refuses to grow up as I grow old. I have collaborated and invested in hundreds of projects throughout my career in multiple industries such as; technology, healthcare, and agriculture. I also have 25 years experience in the marketing industry as a co-founder of an award-winning advertising agency. I will undoubtedly cover a wide variety of topics on my podcast while sharing some really crazy stories and situations that I've been fortunate to witness firsthand. I also have a book coming out in 2025 titled, "Mistakes were Made"

Comments

  1. jamland says

    September 3, 2009 at 7:43 am

    Thanx. This works for me

    Reply
  2. limone says

    September 14, 2009 at 6:34 pm

    thank you, worked well for me too! 🙂

    Reply
  3. home builder seo says

    March 16, 2010 at 11:18 am

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

    Reply
  4. Helen D says

    March 25, 2010 at 11:37 pm

    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.

    Reply
  5. Helen D says

    March 26, 2010 at 11:51 am

    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

    Reply
  6. Generators for Sale says

    March 31, 2010 at 2:57 am

    The script works well. Thank you for this.

    Reply
  7. Aye says

    June 20, 2010 at 6:41 pm

    Thanks, it was very helpful.

    Reply
  8. Web Hosting Service says

    March 16, 2011 at 12:44 pm

    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.

    Reply
  9. Johnny says

    April 22, 2012 at 2:13 pm

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

    Reply

Please Drop Your Questions or CommentsCancel reply

Let’s Connect

  • Email
  • Facebook
  • Instagram
  • LinkedIn
  • Twitter

Recent Updates

  • EP:032 – Cotton Rohrscheib & Diana DeHart
  • Challenges & Opportunities Going into 2025
  • Find us at the 2025 Arkansas Women in Agriculture Conference in Hot Springs, Arkansas
  • Be Sure to Checkout FBN’s Farmers First™  Crop Nutrition & Adjuvant Lineup for 2025
  • What we all need in Dark Times…

Blog Categories

  • Blog (419)
  • Entertainment (376)
  • Faith & Family (147)
  • Farm & Business (288)
  • Health & Wellness (33)
  • Marketing & Tech (584)
  • Podcasts (31)
  • Urban Farming (20)
  • Weekend Projects (1)

Listen & Subscribe

Blog Archives

Join the Cotton Club!

 

Content Copyright: 2001-2025
Cotton Rohrscheib | Rohrscheib Capital