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 brand 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.
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?
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!
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.
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.
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.
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.
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.
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.
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: http://www.kikamaca.com/2011/03/show-recent-comments-without-a-plugin/
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:
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?
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.
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.
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
// 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”;
This is amazing thank you… it totally worked! and is kickass! Thanks
You’re welcome! I am happy it work for you. Thanks for letting me know!
A fantastic post! Interesting read. Going to follow this blog closely in the future. Looking forward to the next update!
Regards,
Andre
@Andre: Thanks for the good words! You’re most welcome!
Great tutorial! And very useful. I’ve been looking everywhere for this.
Thanks a bunch
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.
Hey Pavl, thanks very much for your nice words.
Wow man, thats gr8. I personally suggest all peoples not using too much plugins and take the taste of this kinda WordPress code snippets.
Hi, that’s what I was looking for!
Is it possible to show comments from a specific category only?
Thanks!
Sorry, Kurt, that’s not possible right now.
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?
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.
Thanks for the tutorial and code snippet! Looked everywhere for this and finally came across your site… Bookmarked!
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.
How can I put the post title rather than comment’s text?
PERFECT! Works like a charm.
I was test it and work without any worries
There’s no
functions.phpin my WordPress theme files. Do you know what I should do? I really want this to work.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.
How do I add the date/time and category to comment?
Muchas gracias por esa función! me sirvió un montón! gracias!
——————————————————-
thanks a lot for that function! it really helped me! thanks!
Thanks for the function. Like others, I wanted to display a date with each comment. So I modified the code slightly.
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.
Cheers!!!
Gabrielle, it seem like you did not get the main code to functions.php properly. Can you double-check it and let me know?
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
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.
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
Yes, there is a space there lol – and thanks!
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.
Hi again,
I found out I needed to use user_id != ’1′ and now it works.
Thank you, this helped me.
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.
I was test it and work without any worries
how can i show 10 recent comments?
copied your codes, but mine is showing only 6..
appreciate your help
thanks.
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!
If I use it in the loop it says ” cannot redeclare … ”
How can I fix this?
Cheers,
Ciprian
Try to place the function caller blow the loop.
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.
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.
how do I display the comments without admin’s comment. I want to hide my own comment and just display other’s comments.
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.
Thanks!
Is there any specific advantage of using the code instead of the plugin that allows showing recent comments on your blog?
It’s just more customizable, that’s all.
hi, great piece of code! I have a question, how can I show the post title for each comment?
Thanks!
Dani
Cool. I am wondering–what’s the advantage of doing it this way as opposed to just using the widget? Just curious.
It’s just more customizable.
Gotcha! Thanks!
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.
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,
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:
http://www.kikamaca.com/2011/03/show-recent-comments-without-a-plugin/
Thanks Banago for this wonderful base:)
Dude, bloody brilliant! Worked a treat!
@mgpwr
Thanks for stopping by
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?
That will be answered on my next post, How to shorten anything on WordPress. Stay tuned and thanks for the comment.
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?
Yes, that can be done too.
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.
Thank you so much…This helped me a lot…
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.
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!?
thanks
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.
thanks!
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.
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
Yep, this is a comment that is shown through the brand new recent comments function. Isn’t that cool
// 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
Khawar, please try the updated code.
Work like magic !
Thanks for sharing that