
There’s plugins and widgets that allows you to list recent comments on your WordPress blog. But you know it, I like to avoid using many plugins and enjoy understand how things works. Here’s a nice code to display recent comments on your blog.

There’s plugins and widgets that allows you to list recent comments on your WordPress blog. But you know it, I like to avoid using many plugins and enjoy understand how things works. Here’s a nice code to display recent comments on your blog.
To use this hack, simply paste this code anywhere on your theme files.
<?php
global $wpdb;
$sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID, comment_post_ID, comment_author, comment_date_gmt, comment_approved, comment_type,comment_author_url, SUBSTRING(comment_content,1,30) AS com_excerpt FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) WHERE comment_approved = '1' AND comment_type = '' AND post_password = '' ORDER BY comment_date_gmt DESC LIMIT 10";
$comments = $wpdb->get_results($sql);
$output = $pre_HTML;
$output .= "\n<ul>";
foreach ($comments as $comment) {
$output .= "\n<li>".strip_tags($comment->comment_author) .":" . "<a href=\"" . get_permalink($comment->ID)."#comment-" . $comment->comment_ID . "\" title=\"on ".$comment->post_title . "\">" . strip_tags($comment->com_excerpt)."</a></li>";
}
$output .= "\n</ul>";
$output .= $post_HTML;
echo $output;
?>
This code will list the 10 most recent comments. If you want to list more or less comments, simply change the "10" in the $sql variable.
Credits goes to Kyle Eslick for this awesome recipe!
By the way, have you checked out Nomeblog, a free WordPress theme created by Alex Denning (Author on my other blog Cats Who Code)
40 Responses
Wordpress should start adding these functions by default now.
The best place for this is usually in the sidebar, of which the widget is already there just for this purpose.
Hi,
Is there a way to add this inside “the loop” and display the last 2 comments from each post ?
I agree with Christina. Some of these code snippets can better be accomplished with widgets or plugins. Tossing code directly into the theme makes for difficult transitions and upgrades.
Here is another recipe who to do that: http://www.wplancer.com/how-to-display-recent-comments-without-using-a-plugin-or-widget/
David and Christina are both right, but sometimes you have to be wrong to keep the client happy –
The built in recent comment widget doesnt offer a huge amount of flexibility in what is shown (and by not a huge amount, I mean none), so if you want to display something other than the default (specific length of text from the comment, commenters website, etc etc), this can be useful.
That said, throwing this directly in your theme files is just bad form – at the very least, wrap it in a function and put it in functions.php
Agree with Peter. Widgets are cools, but sometimes they don’t offer as much flexibility as you need.
And anyways, even if it can be better for some reason to use a widget, I still think that it is good to know how the whole code works
i made great use out of this one. thanks for sharing!
Great recipe! But I’d like the comment excerpt to have more words, is it possible? Thanks!
@ Harvey:
Within the code, after the $sql =, find: SUBSTRING(comment_content,1,30).
The “30″ in there is the number of characters that will be displayed in the recent comment. Increase it to whatever number you like to make the excerpt larger.
That’s so cool! Exactly what I was looking for, thanks a lot!
Just a question: If you want to change the hyperlinks from the comments to the author (just the opposite way), how do I have to change the code? (I’m sorry, I always get kind of confused when it comes to php->I’m definitely no expert
)
And how about if I want three dots at the end to show that the comment continues? Like: JUICEDaniel: blablabla…
Great recipe!
However, it doesn’t seem to scroll down to the selected comment when clicking the link outside its respective post, like from the homepage for example. When I click the link, it just goes to the top of the post.
Does anyone knows how I could have it scroll down to the comment when the link is clicked?
Cheers!
@JUICEDaniel:
You can change this line:
$output .= “\n”.strip_tags($comment->comment_author) .”:” . “ID).”#comment-” . $comment->comment_ID . “\” title=\”on “.$comment->post_title . “\”>” . strip_tags($comment->com_excerpt).”“;
To:
$output .= “\n”.strip_tags($comment->comment_author) .”:” . “ID).”#comment-” . $comment->comment_ID . “\” title=\”on “.$comment->post_title . “\”>” . strip_tags($comment->com_excerpt).”…“;
Noticed the changes here: “…“
@ Aldi: Thanks a lot, it works fine! And how about switching the from the comment to the commentator? (That was my first question, “If you want to change the hyperlinks from the comments to the author (just the opposite way), how do I have to change the code?”)
@JUICEDaniel:
That should be quite straight forward. Just change this:
$output .= “\n”.strip_tags($comment->comment_author) .”:” . “ID).”#comment-” . $comment->comment_ID . “\” title=\”on “.$comment->post_title . “\”>” . strip_tags($comment->com_excerpt).”…“;
To:
$output .= “\nID).”#comment-” . $comment->comment_ID . “\” title=\”on “.$comment->post_title . “\”>”.strip_tags($comment->comment_author) .”:” . strip_tags($comment->com_excerpt).”…”;
Is that what you want?
It doesn’t work. I changed it to
$output .= “\nID).”#comment-” . $comment->comment_ID . “\” title=\”on “.$comment->post_title . “\”>”.strip_tags($comment->comment_author) .”:” . strip_tags($comment->com_excerpt).”…”;
And this error occured:
Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /(……..)/wp-content/plugins/php-code-widget/execphp.php(37) : eval()’d code on line 9
@JUICEDaniel:
I think you got it a bit messed up. Try this:
$output .= “\nID).”#comment-” . $comment->comment_ID . “\” rel=\”nofollow\” title=\”on “.$comment->post_title . “\”>”.strip_tags($comment->comment_author) . “:” . strip_tags($comment->com_excerpt).”…”;
Why don’t the codes in the comments above show up well?
Thanks for your help, but unfortunately it still does not work, same error (as above). (And it has no included, which I think, is necessary?
The original code was:
$output .= “\n”.strip_tags($comment->comment_author) .”:” . “ID).”#comment-” . $comment->comment_ID . “\” title=\”on “.$comment->post_title . “\”>” . strip_tags($comment->com_excerpt).”“;
And the code I use (and works fine) is:
$output .= “\n”.strip_tags($comment->comment_author) .”: ” . “ID).”#comment-” . $comment->comment_ID . “\” title=\”on “.$comment->post_title . “\”>” . strip_tags($comment->com_excerpt).”…“;
Now I only want to change the to the front, as I said.
That’s funny… it works when I tested it. In fact, the code that you claim to work doesn’t look right at all, like your placement of
rel="nofollow".ID).”#comment-” . $comment->comment_ID
This is all wrong.
Did you put the code exactly as I have showed it?
$output .= “\nID).”#comment-” . $comment->comment_ID . “\” rel=\”nofollow\” title=\”on “.$comment->post_title . “\”>”.strip_tags($comment->comment_author) .” : ” . strip_tags($comment->com_excerpt).”…”;
I looked in your website and it seemed to work on the footer (“Neueste Kommentare”).
That’s funny… it works when I tested it. In fact, the code that you claim to work doesn’t look right at all, like your placement of
rel="nofollow".ID)."#comment-" . $comment->comment_IDThis is all wrong.
Did you put the code exactly as I have showed it?
$output .= "\nID)."#comment-" . $comment->comment_ID . "\" rel=\"nofollow\" title=\"on ".$comment->post_title . "\">".strip_tags($comment->comment_author) ." : " . strip_tags($comment->com_excerpt)."...";I looked in your website and it seemed to work on the footer (“Neueste Kommentare”).
That’s funny… it works when I tested it. In fact, the code that you claim to work doesn’t look right at all, like your placement of
rel="nofollow".<a href="" rel="nofollow">ID)."#comment-" . $comment->comment_IDThis is all wrong.
Did you put the code exactly as I have showed it?
$output .= "\n<a href=\"" . get_permalink($comment->ID)."#comment-" . $comment->comment_ID . "\" rel=\"nofollow\" title=\"on ".$comment->post_title . "\">".strip_tags($comment->comment_author) ."</a> : " . strip_tags($comment->com_excerpt)."...";I looked in your website and it seemed to work on the footer (”Neueste Kommentare”).
(PS: Sorry for sending this message multiple times. I’m trying to make the code appear right.)
Is it possible to paginate the comments?
How can we add gravatars to recent comments??
So, this is nice. Except one thing which I find weird. The name of the author isn’t linked to the comment in the latest comment listed? Isn’t that really weird or have I messed it up somehow? Also, thanks for showing how to fix … but how can I get it to work better? If there is a space it will be … and not… which also looks weird.
Man, if a coding this short replaces a whole plugin, kinnda makes you wonder what else is there on the plugins.
I use something similar to this, and it’s working great. The thing is, I’d like to know if there is an option for setting the length of the excerpt based on wordcount instead of lettercount.
With com_excerpt,1,10 it shows: “Opera is a gr…”
I would like it showing e.g. 4 words always: “Opera is a grizzlybear …”
No matter how long the words are: “I am a bear …”
or “Bananarepublicans operating excellent sodamachines …”
Trackbacks: