Without a Plugin: Display Recent Comments

This post was originally published on April 9, 2009 and is now being being republished as it has been entirely revised. The original method is removed and now replaced by a new one.

WordPress ships with a Recent Comments widget, which is very handy, but on the other hand does not give you much freedom in terms of look and feel. There are plugins out there with will offer some more functionallity, but you sometimes feel you just want to free yourself form any limitations. So here I present the recent comments function. Let’s get our hands dirty.

Here is the function that you need to put in your functions.php file.

The function has three arguments that allow you to specify the number of comments that you want to show, defaulting to 5; how long you want the comment body text to be, defaulting in 120 characters; and how big you want your gravatar to be, from 0 to 100, defaulting to 48.

Next you want to call the function from the sidebar.php of your theme or wherever else you want them to show on your site. You can do it like this:

And here I’m offering you some boilerplate CSS styles to make your recent comments look good.

I wish you find this article helpful and please make sure to subscribe as more stuff is coming in the Without a Plugin series.

Baki Goxhaj
I’m a web consultant specializing in WordPress. I love elegant solutions, quality code and a good laughter.
You may also like
wordpress custom field example
Overwrite Default Post Permalinks
Pack your WordPress theme with the necessary CSS
  • Livia Ochmann Sep 20,2015 at 4:15 pm

    Thank you very much 😀 It’s working and looks fine.

    • Baki Goxhaj Sep 20,2015 at 6:09 pm

      Really happy to hear that. Keep coding! :)

  • Johan Mar 28,2015 at 7:08 am

    Hi! First of all thank you for the great website!!!

    I would like to extend my current default “Recent Comment Widget” with a date and time. Is that possible?

  • Touraj Feb 13,2015 at 9:41 am

    very very thanks

  • Mani Fred Nov 20,2014 at 3:24 pm


    ty for the great code! But I have a general problem. Every time I put some code in the function.php it works on the frontend but backend isn’t available anymore… only a blank pages appears. The only feature I use in function.php is

    After this I paste your code. Works on frontend, but backend is gone …. would be great if you can help me :)

    • Baki Goxhaj Nov 20,2014 at 5:52 pm

      Hey Mani,

      I cannot help of you don’t tell me what actual error you get. You have to turn WP_DEBUG to true in your config.php and see what you get. Thanks for stopping by.

  • Bien Thuy Oct 19,2014 at 4:21 am

    Can we add it as dynamic sidebar?

  • Hoc Seo Jul 24,2014 at 1:57 am

    Thank you so much!
    But i can’t show comment date with my code:

    get_comment_date( $comment->comment_ID);

  • Dante May 11,2014 at 1:58 am

    Hey! Thank you veeeery much, I hate to use plugin for small things on wordpress. Sure I’ll subscribe to your blog.

    • Baki Goxhaj May 24,2014 at 8:37 am

      I share the same feeling. Thanks very much.

  • Acan Apr 30,2014 at 2:49 pm

    Hi Baki, i think you forget this line ‘status’ => ‘approve’, . Without this, all comments displayed before through moderation. Anyway thanks again..

  • Jonathan Apr 23,2014 at 5:09 am

    Hey Everyone,

    Saw a question about showing the post title on here. It’s pretty simple. Add this code wherever you want in the list of “$comm”s in the original code.

    $comm .= ‘Post: ‘ . get_the_title( $comment->comment_post_ID ) . ”;

    Hope this helps!

    • Baki Goxhaj Apr 23,2014 at 4:45 pm

      Hey Jonathan, thanks for providing this.

  • ramlan Apr 7,2014 at 6:00 am

    Thank you my friend. Thats code help me, fully. :p

  • thuan Mar 7,2014 at 4:24 pm

    Can you help me add date after comment text ? thank you very much.

  • Naomi Jul 6,2013 at 2:49 pm

    Nice!! Just a few questions, how can I adjust the comment text that it won’t break off in the middle? That it displays full words and add … afterwards? And how can I add a read on link after the comment instead of displaying the author? Is that possible?

    • Baki Goxhaj Jul 8,2013 at 7:15 pm

      Naomi, it is very possible, but it’s outside the scope of the article. I could help you with your changes in private if you would like.

  • Roland Jun 30,2013 at 3:12 am

    Thank for your help it works fantastic, but there is a little problem linking to the entire comment, when i click the the name link tag it doesn’t work.
    please any suggestion let us know,

    Thank in advanced.

    • Baki Goxhaj Jul 6,2013 at 7:21 pm

      You will have to just adjust the code so that you can wrap the parts you want in a link. Take how I produce the link there as an example.

  • Dhruv Jun 27,2013 at 10:52 am

    Your method seems to be good but I don’t understand one thing. Why one need to mess up with the coding when already plugins are available. What’s the benefit of messing with coding over using the plugins. Thanks.

    • Baki Goxhaj Jun 27,2013 at 1:19 pm

      Some websites won’t be happy with what plugins have to offer – some people want more control.

  • mn9or Mar 18,2013 at 7:30 am

    Work like magic !

    Thanks for sharing that :)

  • Khawar Junjoi Mar 12,2013 at 9:37 am

    It’s not working when want to display on specific category.

    • Baki Goxhaj Mar 12,2013 at 10:53 am

      Khawar, please try the updated code.

  • Baki Goxhaj Dec 22,2012 at 3:56 pm

    Yep, this is a comment that is shown through the brand new recent comments function. Isn’t that cool :)

  • forhad Jan 26,2012 at 1:14 am

    I am new to wordpress. Just trying to show most recent posts in one widget area. And this is what exactly i am looking for. This article also gives idea on custom query in wordpress. Nice reading ! Bookmarked the blog

  • BrianMI6 Jan 18,2012 at 7:05 pm

    As others have stated, can I tweak this code so that it only pulls in comments from posts in a specific category? That would be a MAJOR help.

  • AY Jan 7,2012 at 11:52 am

    Thank you a lot, that helped me to reduce the number of sql queries by 4. indeed there is an extra space in the code that gave me a little bit of a hassle.


  • […] searching about recent comments and AJAX in wordpress i found these nice examples about recent comments in wordpress without plugin or widget and ajax on wordpress themes and thought about implementing real-time recent comments usign […]

  • Ammar Essa Oct 7,2011 at 12:33 am

    Hi BANAGO, i had comment you about how to avoid pings from appearing as a comment in the list, but it’s look like my comment had been deleted!

    Anyway, i’m still stuck on that and need your help to do it!?


  • Ammar Oct 1,2011 at 12:15 am

    hi, It’s really great job! it’s so useful, THANKS

    i just wanna ask you how to avoid ping from the comment list?

    i’ll be greatful if you can provide me with a solution.

  • Kerala Houseboats Sep 27,2011 at 9:24 am

    Thank you so much…This helped me a lot…

  • Madcore Sep 6,2011 at 1:05 pm

    Hi. This script was a lot usefull to me, but I trying yo modify the code to make it show the commented post title instead the comment excerpt, but I can´t make it. I tried in diferent ways, but my php knowledge is very basic. Can you help me to make this modification?

    Thanks a lot for the script and for your time doing it an reading this.

  • Robert H. Aug 30,2011 at 3:26 am

    Good job! Very useful code.

    Is there a way to customise this so that the comment date and post title can display for each as well?

    • BANAGO Sep 11,2011 at 4:00 pm

      Yes, that can be done too.

  • Spud Aug 17,2011 at 7:14 am

    Thanks! I too had to delete some spaces in front of the ?php’s. I also modified the comment text code slightly to include the link on the text and to give an HTML title with the post title:

    <a href="comment_post_ID ) . ‘#comment-‘ . $comment->comment_ID; ?>”>:
    <a href="comment_post_ID ) . ‘#comment-‘ . $comment->comment_ID; ?>” title=””>comment_content), 0, $comment_len)); ?>

    There’s probably a better way, but I’m feeling pretty proud of myself right about now. I would like to truncate the names of the authors at 10 characters — what is the best way to go about doing this?

    • BANAGO Aug 17,2011 at 8:31 am

      I would like to truncate the names of the authors at 10 characters — what is the best way to go about doing this?

      That will be answered on my next post, How to shorten anything on WordPress. Stay tuned and thanks for the comment.

  • Sheffield Website Design Apr 12,2011 at 12:57 pm

    Dude, bloody brilliant! Worked a treat! :)


    • BANAGO Apr 12,2011 at 1:01 pm

      Thanks for stopping by :)

  • Kika Mar 19,2011 at 8:21 pm

    Like so many people here we wanted to show the date and post title in our commentary as well. So we modified the original code a bit and made it work in our blog. The full article how to add the date and post title can be found in our post:

    Thanks Banago for this wonderful base:)

  • […] trying to find to way to do is for our own blog. After some googling we stumbled upon WPlancer and BANAGO’s post that gave us the base for the function. That is the beauty of open-source: Spreading awesomeness! […]

  • Shraddhananda Feb 6,2011 at 5:59 pm

    Hi Bango,

    Thanks a lot in advance.

    Is there any way where recent comment on each category will show in their respective category page.

    Lets assume there are two categories named Math and Science and we had received comment for Math only. So on Math page the comment will appear and on science no comments will appear.

    Implementing this is showing the latest comment everywhere irrespective of categories.

    Best regards,

  • Fily Feb 5,2011 at 10:35 pm

    Thanks a lot for the code. It worked great after deleting four or five spaces in the <?php ( < ?php delete this space, this is a no, no) tags.

  • Jennifer Jan 10,2011 at 11:37 pm

    Cool. I am wondering–what’s the advantage of doing it this way as opposed to just using the widget? Just curious.

    • BANAGO Jan 11,2011 at 8:40 am

      It’s just more customizable.

  • daniel Dec 27,2010 at 2:17 pm

    hi, great piece of code! I have a question, how can I show the post title for each comment?

  • Mishelli Nov 28,2010 at 4:47 pm

    Is there any specific advantage of using the code instead of the plugin that allows showing recent comments on your blog?

    • BANAGO Jan 11,2011 at 8:41 am

      It’s just more customizable, that’s all.

  • Ajay Nov 23,2010 at 3:25 pm


    Is it possible to enable paging, seeing as the number of comments is set to 10. it would be nice to scroll through previous comments too.


  • Azah Oct 23,2010 at 7:49 am

    how do I display the comments without admin’s comment. I want to hide my own comment and just display other’s comments.

  • Dan Oct 15,2010 at 1:49 am

    It was necessary to change the script a little but we managed to make it work properly. I use it on my blog, you can see this script on my index page, in bottom of the website… Thanks for the info, I have been helpful.

  • Julian Jul 27,2010 at 5:56 am

    Thanks for a great script! Please can someone tell me how to get and display the comment’s date as well. I have been trying to use get_comment_date and then echo comment_date but they don’t show anything. Thanks.

  • Cippo Jul 19,2010 at 11:41 pm

    If I use it in the loop it says ” cannot redeclare … ”

    How can I fix this?


    • BANAGO Jul 20,2010 at 8:29 am

      Try to place the function caller blow the loop.

  • BANAGO Jun 24,2010 at 4:09 pm

    Sure thing, here is the trick. The code I provided uses this code to call the comments:

    Do you see the “6” there? Just replace it with your number, in this case 10. Good luck!

  • Hayley Jun 24,2010 at 2:05 pm

    how can i show 10 recent comments?
    copied your codes, but mine is showing only 6..

    appreciate your help :)


  • Advenda May 16,2010 at 8:44 am

    I was test it and work without any worries

  • mandekira May 10,2010 at 5:04 am

    Hi! Please I’ll like you to help me on how I can display most recent comment of my website on my homepage or on other pages of my website. I uses PHP AND MYSQL for the comment db. Please help me, I’m new to php and dbs in general. Thank you.

  • Emmy Apr 30,2010 at 9:23 am

    Thank you, this helped me.

  • […] donde el número entre paréntesis es el número de comentarios que queremos que aparezcan. Fuente: Clic para ver Texto Plano […]

  • George Serradinho Mar 26,2010 at 1:13 am

    Hi again,

    I found out I needed to use user_id != ‘1’ and now it works.

  • George Serradinho Mar 26,2010 at 1:01 am


    this is neat and I’m testing it, but i don’t want the admin comments to show. I changed the query a bit to exclude post_author != 1, but the admin comments still show.

    Can you maybe explain to me what needs to be changed/done.

  • BANAGO Feb 21,2010 at 11:08 am

    Yes, there is a space there lol – and thanks!

  • gabrielle Feb 21,2010 at 6:39 am

    Finally I found the problem. The thing is, in the code theres a space between

    “<" and "?php"

    in both codes (to functions and to sidebar)

    great code btw

  • BANAGO Feb 21,2010 at 6:00 am

    Basically, what the error tells you is that you did not put the comments function that you are colling where it belongs at functions.php. If you can let me check your site for a moment, I can do that.

  • gabrielle Feb 21,2010 at 5:32 am

    hi banago

    i actually inserted the code on dreamweaver and then uploaded it by ftp

    but i tried directly from my wp dashboard and didnt work either

    both ways i had the error message

  • gabrielle Feb 21,2010 at 4:32 am

    Hi there,

    I tried to implemented the code but I have the next error message:

    Fatal error: Call to undefined function bg_recent_comments() in /home/…..

    I appreciate if somebody could help me with this.


    • BANAGO Feb 21,2010 at 5:24 am

      Gabrielle, it seem like you did not get the main code to functions.php properly. Can you double-check it and let me know?

  • […] Cómo mostrar Comentarios recientes sin utilizar un plugin o widget – Un excelente tutorial para agregar comentarios recientes de su tema usando el archivo functions.php y de algunas de código dentro de sus archivos de tema. […]

  • Bev Jan 24,2010 at 5:12 pm

    Thanks for the function. Like others, I wanted to display a date with each comment. So I modified the code slightly.

  • Agustín Dec 23,2009 at 4:51 pm

    Muchas gracias por esa función! me sirvió un montón! gracias!


    thanks a lot for that function! it really helped me! thanks!

  • univocus Dec 18,2009 at 9:31 pm

    How do I add the date/time and category to comment?

  • Jen Dec 13,2009 at 6:10 pm

    There’s no functions.php in my WordPress theme files. Do you know what I should do? I really want this to work. :(

    • BANAGO Dec 13,2009 at 6:45 pm

      It’s easy Jen, just create a file and name it functions.php and then paste the code I have provided as is.

      However, the fact that you have not function.php file means that your theme is not widgetized too. You might want to add some widget support to it.

  • Jauhari Dec 13,2009 at 2:58 am

    I was test it and work without any worries 😉

  • chinkone Nov 30,2009 at 12:57 pm

    PERFECT! Works like a charm.

  • gilkudik Nov 25,2009 at 5:10 am

    How can I put the post title rather than comment’s text?

  • […] How to Display Recent Comments Without Using a Plugin or Widget » WordPress Freelancer […]

  • […] How to Display Recent Comments Without Using a Plugin or Widget – An excellent tutorial for adding recent comments to your theme using the functions.php file and some code right within your theme files. […]

  • Hillary Sep 6,2009 at 10:43 pm

    Looking to customize this in a few ways. Firstly, would love to only show recent comments from everyone BUT the blog author. Secondly, is there are way to show the date of the comment? What about the title of the post the comment was on?

    Thanks in advance for any suggestions you can offer!

    • Baki Goxhaj Dec 22,2012 at 5:05 pm

      All of those are possible hillary but you have to edit the comment. Perhaps I’ll add the date as an option by default.

  • Philip Downer Aug 12,2009 at 11:30 pm

    Thanks for the tutorial and code snippet! Looked everywhere for this and finally came across your site… Bookmarked!

  • natalia Jul 7,2009 at 12:15 am

    Just what I was looking for, thanks for posting this useful code, sorry to bother, but my PHP skills are not very good, is it possible to echo the date of the comment?

    • Baki Goxhaj Dec 22,2012 at 5:03 pm

      Yes, it is but you have to edit the comment to achieve that. If other people will be instrested in that, I can add it.

  • Kurt Jun 25,2009 at 10:29 am

    Hi, that’s what I was looking for!

    Is it possible to show comments from a specific category only?


    • Baki Goxhaj Dec 22,2012 at 5:02 pm

      Sorry, Kurt, that’s not possible right now.

  • WpDite Jun 1,2009 at 5:16 pm

    Wow man, thats gr8. I personally suggest all peoples not using too much plugins and take the taste of this kinda WordPress code snippets.

  • Baki Goxhaj May 12,2009 at 10:09 pm

    Hey Pavl, thanks very much for your nice words.

  • Pavl Williams May 12,2009 at 8:49 pm

    Thanks Banago, this is exactly what I was looking for.

    I’m surprised this functionality hasn’t been included in wordpress yet – perhaps you should get onto them?

    All the best and keep up the great work.

  • kramero May 3,2009 at 7:32 am

    Great tutorial! And very useful. I’ve been looking everywhere for this.

    Thanks a bunch :)

  • BANAGO May 1,2009 at 8:21 am

    @Andre: Thanks for the good words! You’re most welcome!

  • Andre Apr 26,2009 at 7:38 pm

    A fantastic post! Interesting read. Going to follow this blog closely in the future. Looking forward to the next update!


  • BANAGO Apr 16,2009 at 10:21 pm

    You’re welcome! I am happy it work for you. Thanks for letting me know!

  • TJ Apr 16,2009 at 10:11 pm

    This is amazing thank you… it totally worked! and is kickass! Thanks :)

Leave Your Comment

Your Name*
Your Webpage

Your Comment*