Post Pic

How to: List most recent comments

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)

Related Posts

No related posts.

40 Responses

Apr 08 2009 10:51

Wordpress should start adding these functions by default now.

Apr 08 2009 15:30

The best place for this is usually in the sidebar, of which the widget is already there just for this purpose.

Apr 08 2009 18:40

Hi,

Is there a way to add this inside “the loop” and display the last 2 comments from each post ?

Apr 08 2009 20:31

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.

Apr 09 2009 19:56
Apr 10 2009 05:05

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

Apr 10 2009 10:04

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 :)

May 31 2009 14:06

i made great use out of this one. thanks for sharing!

Jun 25 2009 12:01

Great recipe! But I’d like the comment excerpt to have more words, is it possible? Thanks!

Aug 02 2009 05:48

@ 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. :)

Aug 14 2009 13:46

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 ;) )

Aug 14 2009 13:49

And how about if I want three dots at the end to show that the comment continues? Like: JUICEDaniel: blablabla…

Aug 17 2009 09:47

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!

Aug 17 2009 11:37

@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: “…“

Aug 17 2009 14:50
Aug 17 2009 15:55

@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?

Aug 17 2009 19:57

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

Aug 18 2009 02:34

@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).”…”;

Aug 18 2009 02:44

Why don’t the codes in the comments above show up well?

Aug 19 2009 12:19

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.

Aug 20 2009 02:35

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”).

Aug 20 2009 02:37

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”).

Aug 20 2009 02:55

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_ID

This 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.)

Sep 02 2009 00:07

Is it possible to paginate the comments?

Sep 20 2009 15:38

How can we add gravatars to recent comments??

Nov 22 2009 01:12

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.

Dec 18 2009 14:51

Man, if a coding this short replaces a whole plugin, kinnda makes you wonder what else is there on the plugins.

Jan 10 2010 18:49

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:

Leave a Comment

* Name, Email, Comment are Required