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.

84 thoughts on “Without a Plugin: Display Recent Comments

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


  2. 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.

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

  4. 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?

  5. 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!

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

    • 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.

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


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

  7. 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.


  8. 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

  9. 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.

  10. 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

  11. Hi,

    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.

  12. 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.

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

    appreciate your help :)


  14. 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!

  15. 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.

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

  17. Excellent.

    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.


  18. 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.

  19. 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,

  20. 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?

    • 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.

  21. 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?

  22. 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.

  23. 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.

  24. 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!?


  25. 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.


  26. 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.

  27. 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

  28. // Posts per page setting
    $ppp = get_option(‘posts_per_page’); // either use the WordPress global Posts per page setting or set a custom one like $ppp = 10;
    $custom_offset = 0; // If you are dealing with your custom pagination, then you can calculate the value of this offset using a formula

    // category (can be a parent category)
    $category_parent = 3;

    // lets fetch sub categories of this category and build an array
    $categories = get_terms( ‘category’, array( ‘child_of’ => $category_parent, ‘hide_empty’ => false ) );
    $category_list = array( $category_parent );
    foreach( $categories as $term ) {
    $category_list[] = (int) $term->term_id;

    // fetch posts in all those categories
    $posts = get_objects_in_term( $category_list, ‘category’ );

    $sql = “SELECT comment_ID, comment_date, comment_content, comment_post_ID
    FROM {$wpdb->comments} WHERE
    comment_post_ID in (“.implode(‘,’, $posts).”) AND comment_approved = 1
    ORDER by comment_date DESC LIMIT $ppp OFFSET $custom_offset”;

    $comments_list = $wpdb->get_results( $sql );

    if ( count( $comments_list ) > 0 ) {
    $date_format = get_option( ‘date_format’ );
    echo ”;
    foreach ( $comments_list as $comment ) {
    echo ‘Comment: ‘.substr( $comment->comment_content, 0, 50 ).’..’.date( $date_format, strtotime( $comment->comment_date ) ).’Post: comment_post_ID ).’”>’.get_the_title( $comment->comment_post_ID ).’‘;
    echo ”;
    } else {
    echo ‘No comments’;

    but it’s not working when want to display on specific category

  29. 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.

  30. 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.

    • 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.

  31. 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?

Leave a Reply

Your email address will not be published. Required fields are marked *