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.
0 responses to “Without a Plugin: Display Recent Comments”
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 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. […]
[…] How to Display Recent Comments Without Using a Plugin or Widget » WordPress Freelancer […]
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.php
in 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.
[…] 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. […]
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.
[…] donde el número entre paréntesis es el número de comentarios que queremos que aparezcan. Fuente: https://wplancer.com/how-to-display-recent-comments-without-using-a-plugin-or-widget/ Clic para ver Texto Plano […]
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,
[…] 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! […]
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
[…] 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 […]
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 🙂
It’s not working when want to display on specific category.
Khawar, please try the updated code.
Work like magic !
Thanks for sharing that 🙂
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.
Some websites won’t be happy with what plugins have to offer – some people want more control.
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.
Roland
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.
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?
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.
Can you help me add date after comment text ? thank you very much.
Thank you my friend. Thats code help me, fully. :p
I’m happy to hear that – thanks. 🙂
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!
Hey Jonathan, thanks for providing this.
Hi Baki, i think you forget this line ‘status’ => ‘approve’, . Without this, all comments displayed before through moderation. Anyway thanks again..
Hey! Thank you veeeery much, I hate to use plugin for small things on wordpress. Sure I’ll subscribe to your blog.
I share the same feeling. Thanks very much.
Thank you so much!
But i can’t show comment date with my code:
get_comment_date( $comment->comment_ID);
Can we add it as dynamic sidebar?
Hi,
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 🙂
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.
very very thanks
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?
Very nice “plugin” 🙂
and ty Jonathan for title.
for date add under “$comm =[…]” :
” $d = “l, F jS, Y”;”
and in loop :
$comm .= get_comment_date( $d, $comment->comment_ID);
https://codex.wordpress.org/Function_Reference/get_comment_date
Hope this helps 🙂 !
Thank you very much 😀 It’s working and looks fine.
Really happy to hear that. Keep coding! 🙂
Hi, I would suggest just using a plugin for this if you’re not comfortable with PHP. We have just released a free plugin in the WordPress plugin repository called Better Recent Comments. It allows you to display recent comments in any post, page or widget with options to include the avatar, comment excerpt etc. I hope this is helpful for your readers.
Thanks Baki,
I believe there is an ul missing surrounding the list items. Apart from that it is working nicely, thanks.
otti
I am working on daily plugins but actually its not working so far.
thank you so very much!
how to prevent duplication of recent comments links