1

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!

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.

3

Use $comment->comment_post_ID instead of $comment->post_id.

Also you don't need get_post(...). Just use get_the_title instead of your $post_obj and related code...

  • I am still getting the same result. All with the same title. Though it changes the post title according what is currently viewed. I updated the code.. – Joann Jan 9 '11 at 9:57
  • I still see the same code there, at least in your updated question. Read my answer again, especially the 1st line – onetrickpony Jan 9 '11 at 11:36
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

  • I tried it but it didn't work :-( – Joann Jan 9 '11 at 11:48
  • Hmmm.. and right before your widget? (i.e. at the top of the code above) – Noel Tock Jan 9 '11 at 12:12
  • Yah. I am using Thematic and just found out it has its own Recent comments widget but without gravatar. I am guessing it's causing conflicts. – Joann Jan 9 '11 at 12:58
  • Ahh yes, that would explain things. Not familiar with Thematic unfortunately :/ – Noel Tock Jan 9 '11 at 13:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.