I first got the idea for this useful WordPress code snippet whilst browsing video gaming blog Destructoid. Basically, I wanted to extract the latest comment from each post and display a short excerpt of it on the blog homepage (index.php). I've included a screenshot below to give you an idea of the final result:
It'll take a bit of CSS wrangling to get it to look exactly like that but provided you don't mind getting your hands dirty then I'm sure you can come up with something much more attractive. Anywho, simply add the following code to your theme's index.php and place the following code under <?php the_content ?>:
<?php $comment_array = array_reverse(get_approved_comments($wp_query->post->ID)); $count = 1; if ($comment_array) { foreach($comment_array as $comment) { if ($count++ <= 1) { ?>
<div>Latest comment by <?php comment_author(); ?>:</div>
<em><?php comment_excerpt(); ?></em>
<?php } } } ?>
And that's all there is to it. One thing to bear in mind, however, is that for each latest comment excerpt that is displayed, it will require one database query. So, if you've opted to display, say, 50 posts per page then you'll be hit with an extra 50+ database queries for each page load. Crikey. You might, however, find solace in the fact that this code "degrades gracefully" in that it will not require an extra page load if a post does not contain any comments.
