How to: Recent Comments without Plugin
In the ongoing creation process of the new Paintbits theme (Lumen v2), I was researching a way to implement some functionality to my footer, but without the use of plugins. One of the features I want displayed in it it’s some recent comments by the users. I found some solutions, but none satisfied my needs completely.
With the help of the WordPress default files and the WordPress Codex and some PHP I ended up with a little code snippet that do exactly what I want: A certain amount of recent comments, filtering the tracbacks and pingbacks, with a preview of the comment when hovering over the link.
The code is fairly simple.
The Code
<div class="footer_recent_comments">
<h2>Recent Comments</h2>
<ul>
< ?php
$comments = get_comments('number=10&amp;status=approve');
$true_comment_count = 0;
foreach($comments as $comment) :
?>
< ?php $comment_type = get_comment_type(); ?>
< ?php if($comment_type == 'comment') { ?>
< ?php $true_comment_count = $true_comment_count +1; ?>
< ?php $comm_title = get_the_title($comment->comment_post_ID);?>
< ?php $comm_link = get_comment_link($comment->comment_ID);?>
< ?php $comm_comm_temp = get_comment($comment->comment_ID,ARRAY_A);?>
< ?php $comm_content = $comm_comm_temp['comment_content'];?>
<li><span class="footer_comm_author">< ?php echo($comment->comment_author)?></span> on <a href="<?php echo($comm_link)?>" title="< ?php comment_excerpt(); ?>"> < ?php echo $comm_title?> </a>
</li>
< ?php } ?>
< ?php if($true_comment_count == 5) {break;} ?>
< ?php endforeach;?>
</ul>
</div>
The code is surrounded by a div with the class .footer_recent_comments so you can style and position the output in your footer / sidebar or wherever you want to place it.
If you want to increase the number of comments displayed, you will need to change the variable true_comment_count == 5 to the value you want, and at the top of the code get_comments(‘number=10&status=approve’); to a number bigger than the number of comments you want. Why? Because the get_comments fetches for example 10 comments, but some of these might be trackbacks and we don’t want to display those. So within the loop we will each comment to see if they’re a real comment. If that’s the case we will display it, if not we ignore it, when we reach the number of “true” comments we want in the 10 we’ve read from the database, in this example that’s 5, we will interrupt the cycle and display the comments. I hope this is clear to you.
Just as an example, I’ll show you the CSS code I’ve used, to style the Recent Comments code I’m using in the footer.
The CSS Code
#footer
{
clear: both;
width: 1022px;
height: 230px;
background: url("images/footer_bg.jpg") repeat-y;
display:block;
}
#footer-content
{
clear:both;
padding: 25px 0;
margin: 0 31px 0 31px;
color: #7b7b7b;
}
#footer-content a, #footer-content a:visited
{
color: #7b7b7b;
}
#footer-content a:hover
{
color: #0592f4;
}
#footer-content div {
margin: 0 0 0 20px;
float: left;
}
#footer-content h2
{
font-family: Arial, Verdana, Helvetica;
font-size: 1.3em;
color:#efefef;
line-height: 1.1em;
letter-spacing: 1px;
padding: 0px 10px 10px 0;
margin: 0px 0 0 0;
}
#footer-content ul
{
text-align: left;
padding: 0 0 0 0;
}
#footer-content ul li
{
list-style: none;
padding: 5px 0 5px 20px;
margin: 0;
border-bottom: 1px solid #242424;
background: url("images/bullet_go_blue.png") left center no-repeat;
line-height:14px;
}
#footer-content ul li:hover
{
background: #121212 url("images/bullet_go_purple.png") left center no-repeat;
}
#footer-content ul li a
{
font-size: 1.0em;
}
#footer-content .footer_recent_comments
{
float: left;
width: 290px;
height:100%;
}
#footer-content .footer_comm_author
{
font-weight:bold;
color:#aaaaaa;
}
Hope this little bits of code can help you in some way in your present or future blogging endeavourers. You can see it in action on my footer.




Funkeh, though I only understand the concept, not the code part of it =]
The end result looks nice too and will certainly be helpful for visitors and yourself to access recent comments – even if there are more convenient ways for admins to do so.
Keep on rollin’
Thank you for the comment, mindseas, aka Providus! :D
Ye I think this can be useful to people trying to improve their WordPress theme without plugins. I Don’t have anything against plugins, but coding your own functions always keep the website cleaner and uncluttered with includes and stuff.
PS – We starting to Raid again Mister, log on your WoW Account ASAP! :p
Indeed.
Logging on…so overrated. It wouldn’t do much good though since the account is frozen at the moment. Been close to reactivating it but I prefer not to yet, maybe after this week if I get everything in order, but no promises.
this is a cool one!
Thank you for this great explanation! I have a question, how can you add a floating gravatar next to each comment??
Floating gravatar to the recent comments?
This is exactly what I needed. Thanks!
Very nice. It’s much better not to crawl wordpress database if there is already wp functions.
It took me 3 hours to find NOTHING about this function in French…
then I tried in English and figured it out in 3 minutes. lol
so thank you very much :-P
ENGLISH; SUPERIOR
thanks for sharing. nice post!
hey, why are these damn spammers just say “nice post” and stuff like that? Are they stupid or something????
very nice blog :D
Excellent, simple and easy to use. Thanks!
It was hard for me, cause I don’t know PHP, but I did it! Thanks for code!
You’re welcome Boldis. :)
Great walk through and simple tutorial.Thanks for sharing this.