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.
< ?php // Get Recet Comments Function function dp_recent_comments($no_comments = 10, $comment_len = 35) { global $wpdb; $request = "SELECT * FROM $wpdb->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(); ?> <li> <a href="<?php echo get_permalink( $comment->comment_post_ID ) . '#comment-' . $comment->comment_ID; ?>">< ?php echo dp_get_author($comment); ?>:</a> < ?php echo strip_tags(substr(apply_filters('get_comment_text', $comment->comment_content), 0, $comment_len)); ?> </li> < ?php ob_end_flush(); } } else { echo '<li>'.__('No comments', 'banago').''; } } function dp_get_author($comment) { $author = ""; if ( empty($comment->comment_author) ) $author = __('Anonymous', 'banago'); else $author = $comment->comment_author; return $author; }?>
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:
function dp_recent_comments($no_comments = 10, $comment_len = 35)
The second is the comment wrapping.
echo '<li>'.__('No comments', 'banago').'</li>';
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.
<div class="custom widget"> <h3>Recent Comments</h3> <ul> < ?php dp_recent_comments(6); ?> </ul> </div>
If you found this short tutorial helpful or your need any sort of help, just write a short comment below. Thanks and good luck!
UPDATE: This has been a quite active post and I can see that a lot of people need this functionality. If you want this on your blog/site, but you do not know how to do that, please drop me a line.
45 people shared their opinion »
Trackbacks
- Blog – Velagapati - 300+ Resources to Help You Become a WordPress Expert
- Blog – Velagapati - 300+ Resources to Help You Become a WordPress Expert
- Blog – Velagapati - 300+ Resources to Help You Become a WordPress Expert
- Mateus Neves, Designer, Wordpress and more » Blog Archive » Meus últimos links de Wordpress no Delicious
- 300+ Resources to Help You Become a WordPress Expert | LeVoltz - iPhone Games, Engineering Projects, Wordpress Themes
- 300多种可帮你成为WordPress专家的资源 | 上海网站建设,上海网络公司
- wordpress – Recursos | Tienda virtual online. Diseño de paginas web. Posicionamiento Seo y Sem













steven
April 13, 2009at 9:43 am
Hi, nice hack. But is there a way you can put “…” on the end?
BANAGO
April 13, 2009at 10:15 am
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.
BANAGO
April 13, 2009at 10:19 am
Perhpas it might be better to replace the “…” with the html symbol & q u o t ; [without spaces]
steven
April 14, 2009at 5:38 pm
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
BANAGO
April 14, 2009at 6:28 pm
What is the code in the 14th line of functions file?
TJ
April 16, 2009at 10:11 pm
This is amazing thank you… it totally worked!!! and is kickass!! thanks
BANAGO
April 16, 2009at 10:21 pm
You’re welcome! I am happy it work for you. Thanks for letting me know!
Andre
April 26, 2009at 7:38 pm
A fantastic post! Interesting read. Going to follow this blog closely in the future. Looking forward to the next update!
Regards
Andre
Get Fit
April 27, 2009at 7:52 pm
Now is the time to get fit!
BANAGO
May 1, 2009at 8:21 am
@Andre: Thanks for the good words! You’re most welcome!
kramero
May 3, 2009at 7:32 am
great tuts..! and very useful…
I’ve been looking everywhere for this..
Thanks a bunch..
Kersten
May 5, 2009at 11:24 am
Attractive
Pavl Williams
May 12, 2009at 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.
BANAGO
May 12, 2009at 10:09 pm
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.
WpDite
June 1, 2009at 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 codes.
@BANAGO
Ya, its really strange that wordpress is not coming with this by default.
Kurt
June 25, 2009at 10:29 am
Hi, that’s what I was looking for!
Is it possible to show comments from a specific category only in ?
Thanks!
natalia
July 7, 2009at 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?
joyologo design shop 2.0
August 2, 2009at 7:42 pm
useful tecnique, thanks for the tut..
Philip Downer
August 12, 2009at 11:30 pm
Thanks for the tutorial and code snippet! Looked everywhere for this and finally came across your site… Bookmarked!
Hillary
September 6, 2009at 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!
gilkudik
November 25, 2009at 5:10 am
How can i put the post title rather than comment’s text?
gilkudik
November 25, 2009at 5:53 am
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}
chinkone
November 30, 2009at 12:57 pm
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]
Jauhari
December 13, 2009at 2:58 am
I was test it and work without any worries
Jen
December 13, 2009at 6:10 pm
theres no functions.php in my wp theme file…
do u know what i can do?
i really want it works.
BANAGO
December 13, 2009at 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.
univocus
December 18, 2009at 9:31 pm
how to add the date/time and category to comment?
Agustín
December 23, 2009at 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!
Bev
January 24, 2010at 5:12 pm
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)); ?>
Bev
January 24, 2010at 5:14 pm
My code didn’t show up, I’ll try again.
[code]
<li class=''>
comment_date);
echo $date;
?>
comment_content)); ?>
[/code]
Bev
January 24, 2010at 5:16 pm
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;
joen baldwin
February 11, 2010at 10:47 pm
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.
gabrielle
February 21, 2010at 4:32 am
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!!!
BANAGO
February 21, 2010at 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?
gabrielle
February 21, 2010at 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
BANAGO
February 21, 2010at 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
February 21, 2010at 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 codre btw
BANAGO
February 21, 2010at 11:08 am
Yes, there is a space there lol – and thanks!