Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 3 down vote accepted

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

share|improve this answer
    
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
    
:-( :-) ;-) Thanks! –  Joann Jan 9 '11 at 13:02

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

share|improve this answer
    
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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