Display recent comments with gravatar and excerpts?

I couldn’t seem to find a plugin with both functionality so I created mine(not a plugin) thru the help of my old friend google. My code works but there’s a small glitch, the title of the posts are the same for all recent comments.

$comments = get_comments('status=approve&number=5'); ?>
<ul> <?php 
foreach ($comments as $comment) { 
    $post_obj = get_post($comment->post_id); 
    $title = $post_obj->post_title; ?>
    <li><?php
        echo get_avatar( $comment, '42' );
        echo strip_tags($comment->comment_author); ?>&nbsp;<?php
        echo wp_html_excerpt( $comment->comment_content, 35 ); ?><br />
        <a href="<?php echo get_permalink($comment->post_id); ?>" rel="external nofollow" title="<?php echo $title; ?>"> <?php echo $title; ?> </a> 
    </li>
<?php }  ?>
</ul>

Any help would be appreciated. Thanks!

Read More

UPDATE:
Here’s the current code. Edited according to Ambitious Amoeba.

$comments = get_comments('status=approve&number=5'); ?>
<ul>
<?php foreach ($comments as $comment) { ?>
    <li><?php
        $title = get_the_title($comment->post_ID);
        echo get_avatar( $comment, '42' );
        echo strip_tags($comment->comment_author);
        ?>&nbsp;<?php
        echo wp_html_excerpt( $comment->comment_content, 35 ); ?>
        <br /><a href="<?php echo get_permalink($comment->post_ID); ?>" rel="external nofollow" title="<?php echo $title; ?>"> <?php echo $title; ?> </a>
    </li>
<?php }  ?>

Still getting the same result. All with the same title. Though it changes the post title according what is currently viewed.

Related posts

Leave a Reply

2 comments

  1. Do you have wp_reset_query(); at the bottom of your single post/page? I’m assuming that the previous query is flowing over into your widget query and that the reset mentioned above is missing from the single template.

    Cheers

    Noel