How to Display Recent Comments Without Using a Plugin or Widget
Yes, that’s right, you can have comments displayed in your blog or site running on WordPress without using neither the classic pre-built Recent Comments Widget nor any of the Recent Comments Plugins that are available out there for everybody to use. To do so you need to have two pieces of PHP code with little (X)HTML code around it. We will not deal with CSS styling in this post, sorry. The first part is to be put on your functions.php theme file and the second part wherever you feel like doing so in theme. So, let’s get our hands dirty.Here is the code that you need to put in your functions.php.
{code type=php}
comments”;
$request .= ” JOIN $wpdb->posts ON ID = comment_post_ID”;
$request .= ” WHERE comment_approved = ’1′ AND post_status = ‘publish’ AND post_password =””;
$request .= ” ORDER BY comment_date DESC LIMIT $no_comments”;
$comments = $wpdb->get_results($request);
if ($comments) {
foreach ($comments as $comment) {
ob_start();
?>
comment_content), 0, $comment_len)); ?>
< ?php
ob_end_flush();
}
} else {
echo '
}
}
function dp_get_author($comment) {
$author = “”;
if ( empty($comment->comment_author) )
$author = __(‘Anonymous’, ‘banago’);
else
$author = $comment->comment_author;
return $author;
}?>
{/code}
You need to stop at two moments in the code:
The first one is the comment length. You can change the the length of the comment excerpt that you want to display on your blog by setting the number of the characters here:
{code type=php}
function dp_recent_comments($no_comments = 10, $comment_len = 35)
{/code}
The second is the comment wrapping.
{code type=php}
echo ‘
‘;
{/code}
It is normally wrapped in <li>comment</li> because you are supposed to wrap the comments query in by a ordered or unordered list, as we will see below. However, you can change the wrapping tags to whatever you like.
The final step of this process is querying the comments to display on your desired part of the blog or site. All you have to do now is to put the code below where you like on your theme.
Here is the code to query the comments to display on you theme. Please note that the number in the brackets sets the number of the comment to be displayed – you can change it to fit your needs.
{code type=php}
{/code}
If you found this short tutorial helpful or your need any sort of help, just write a short comment below. Thanks and good luck!















Hi, nice hack. But is there a way you can put “…” on the end?
This worked for me:
You add “…” before and after the second line of PHP in the code below that is on you functions.php file. Let me know if it works for you too.
{code type=php}
<a href="comment_post_ID ) . ‘#comment-’ . $comment->comment_ID; ?>”>:
“comment_content), 0, $comment_len)); ?>”
{/code}
Perhpas it might be better to replace the “…” with the html symbol & q u o t ; [without spaces]
It didn’t work
Parse error: parse error, unexpected ‘=’ in /home/www/hee.awardspace.biz/wordpress/wp-content/themes/default/functions.php on line 14
What is the code in the 14th line of functions file?
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
Now is the time to get fit!
@Andre: Thanks for the good words! You’re most welcome!
great tuts..! and very useful…
I’ve been looking everywhere for this..
Thanks a bunch..
Attractive
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 great words.
It’s strange that this functionality is not included in WordPress core yet. I will see what I can do.
Wow man, thats gr8. I personally suggest all peoples not using too much plugins and take the taste of this kinda wordpress codes.
@BANAGO
Ya, its really strange that wordpress is not coming with this by default.
Hi, that’s what I was looking for!
Is it possible to show comments from a specific category only in ?
Thanks!
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?
useful tecnique, thanks for the tut..
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!
[...] 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 – 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 – 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?
I use:
{code type=php}
foreach ($comments as $comment) {
ob_start();
?>
en:
<a href="comment_post_ID ) . ‘#comment-’ . $comment->comment_ID; ?>”>post_title ?>
comment_content), 0, $comment_len)); ?>
<?php
ob_end_flush();
}
{code}
PERFECT! Works like a charm.
to get the post title, try this:
[code]
<a href="comment_post_ID ) . '#comment-' . $comment->comment_ID; ?>"> on
<a href="comment_post_ID )?>">comment_post_ID )?>
comment_content), 0, $comment_len)); ?>
[/code]
[...] 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. [...]
I was test it and work without any worries
theres no functions.php in my wp theme file…
do u know what i can do?
i really want it works.
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 to 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!
[...] How to Display Recent Comments Without Using a Plugin or Widget (不使用插件或者小工具,如何显示最近评论) – 优秀的教程:使用 functions.php 文件和一些代码在主题上显示最近文章。 [...]
Thanks for the function. Like others, I wanted to display a date with each comment. So I modified the code slightly as shown below, giving the date a class so I can style it in css:
<li class='’>
comment_date);
echo $date;
?>
comment_content)); ?>
My code didn’t show up, I’ll try again.
[code]
<li class=''>
comment_date);
echo $date;
?>
comment_content)); ?>
[/code]
I can’t get the code to display, so I’ll just show the relevant function:
date = mysql2date(get_option(‘date_format’), $comment->comment_date);
echo $date;
I am bare impressed with the article I have just read. I wish the writer of http://www.wplancer.com can continue to provide so much worthwhile information and unforgettable experience to http://www.wplancer.com readers. There is not much to state except the following universal truth: Don’t judge a book by its cover, judge it by its color, its weight, its height, its width, its complextion, and all of its other features. I will be back.
[...] 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 dp_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 codre 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: http://www.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!
[...] Recent Comment Without a Plugin – While not truly a plugin, this code snippet allowed us to remove a plugin that we used to power the one of the most important sections of our site. Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages. [...]
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