Cotton Rohrscheib

The Cotton Club Blog & Podcast

  • Home
  • About
    • Entrepreneurial Journey
  • Blog
    • Faith & Family
    • Marketing & Tech
    • Farm & Business
    • Entertainment
    • Health & Wellness
    • Urban Farming
    • Weekend Projects
  • Podcast
  • Newsletter
  • Media
    • Photo Galleries
    • Video Archives
  • Marketplace
    • eBay
    • Buying Gold & Silver
    • Accoutrements
      • Bags & Briefcases
      • Notebooks
      • Pens
      • Personal Carry
      • Wallets
      • Watches & Jewelry
    • Poshmark
    • Artwork
    • Banknotes & Currency
      • Currency
      • Gold Banknotes
      • Silver Certificates
    • CIvil War Relics
    • Native American Artifacts
    • Rare Books
    • Rare Coins
      • Coin Sets
      • Individual Coins
    • Signs & Advertising
    • Stock Certificates
    • Trading Cards
      • Trading Cards: Autographs
      • Trading Cards: Base
      • Trading Cards: Graded
      • Trading Cards: Raw
      • Trading Cards: Pokémon
      • Trading Cards: Sets
    • Vintage Toys
      • Buddy L
      • Ertl Farm Toys
      • GI Joe
      • Masters of the Universe
      • Hot Wheels
      • Lionel Trains
      • Lunch Boxes
      • Marx Toys
      • NASCAR Die-Cast
      • Schleich Toys
      • Star Wars
      • Tonka Trucks
    • Vintage T-Shirts
    • Vinyl Records
  • Connect
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;?>

Filed Under: Marketing & Tech Tagged With: Wordpress™ Theme Design

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

    Log in to Reply
  2. limone says

    September 14, 2009 at 6:34 pm

    thank you, worked well for me too! 🙂

    Log in to 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!

    Log in to 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.

    Log in to 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

    Log in to Reply
  6. Generators for Sale says

    March 31, 2010 at 2:57 am

    The script works well. Thank you for this.

    Log in to Reply
  7. Aye says

    June 20, 2010 at 6:41 pm

    Thanks, it was very helpful.

    Log in to 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.

    Log in to 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!

    Log in to Reply

Leave a Reply Cancel reply

You must be logged in to post a comment.

  • Email
  • Facebook
  • Instagram
  • LinkedIn
  • Twitter

Recent Updates

  • The Marketplace is now Connected to eBay!
  • Ep034: Cotton Rohrscheib & Tim Volberding
  • I Stay on Track w/ Reminders & Task Lists
  • It’s a Really Dry Start to the Season…
  • Trading Card Grading Services

Blog Categories

  • Blog (440)
  • Entertainment (378)
  • Faith & Family (155)
  • Farm & Business (302)
  • Health & Wellness (38)
  • Marketing & Tech (592)
  • Podcasts (33)
  • Urban Farming (24)
  • Weekend Projects (10)

Blog Archives

Join the Cotton Club!

 

Content Copyright © 2000-2026
Cotton Rohrscheib | Rohrscheib Capital
Disclaimer | Privacy Policy | Account Manager | View Cart

All opinions expressed on this website are 100% Cotton (see my disclaimer). All content, including text, images, and media, are the intellectual property of Rohrscheib Capital unless otherwise noted. To learn how we use your private information, checkout our privacy policy.