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'm pretty sure that this can be solved quite easy, but for some reason I just can't get it to work.

My current code looks like this:

<?php   $comments = get_comments('status=approve&number=5'); ?>
<ul id="recent_comments">
<?php foreach ($comments as $comment) { ?>
<li><p><strong><?php
        $title = get_the_title($comment->post_ID);
        echo get_avatar( $comment, '45' );
echo strip_tags($comment->comment_author); ?></strong>&nbsp;commented on <a href="<?php echo get_permalink($comment->post_ID); ?>" rel="external nofollow" title="<?php echo $title; ?>"> <?php echo $title; ?></a>: <?php echo wp_html_excerpt( $comment->comment_content, 45 ); ?> (...)</p></li>
<?php }  ?>
</ul>

It works fine, but it only displays the post title and a link to it. I'd like to have the post title displayed, but have it linked directly to the specific comment.

I suppose you'd use something like that:

<a href="<?php echo get_permalink($comment->ID); ?>#comment-<?php echo $comment->comment_ID; ?>" title="on <?php echo $comment->post_title; ?>"></a>

I tried to implement this into the code above somehow. It works, but it suddenly shows the title of a post where the comment doesn't belong to.

Anybody knows how to fix this?

Thanks a lot in advance!

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Instead of $comment->post_ID use $comment->comment_post_ID. Your code will look like following:

<?php   $comments = get_comments('status=approve&number=5'); ?>
<ul id="recent_comments">
<?php foreach ($comments as $comment) { ?>
<li><p><strong><?php
        $title = get_the_title($comment->comment_post_ID);
        echo get_avatar( $comment, '45' );
echo strip_tags($comment->comment_author); ?></strong>&nbsp;commented on <a href="<?php echo get_permalink($comment->comment_post_ID); ?>" rel="external nofollow" title="<?php echo $title; ?>"> <?php echo $title; ?></a>: <?php echo wp_html_excerpt( $comment->comment_content, 45 ); ?> (...)</p></li>
<?php }  ?>
</ul>

You were doing right about adding comment's div id at the end of URL for creating individual comment links. You need to add #comment-<?php echo $comment->comment_ID; ?> at the end of post permalink.

Here is how the anchor tag should look like.

<a href="<?php echo get_permalink($comment->comment_post_ID); ?>#comment-<?php echo $comment->comment_ID; ?>" rel="external nofollow" title="<?php echo $title; ?>"> <?php echo $title; ?></a>
share|improve this answer
    
Hello. Yes, this works, of course, but what I want is not only the link to the post, but also the specific comment as in "http://....post-title/comment-#12". Do you know how to achieve this? And thank you very much :) –  japanworm Sep 16 '11 at 10:52
    
updated my answer –  Hameedullah Khan Sep 16 '11 at 11:48
    
Awesome, thank you so much!! –  japanworm Sep 16 '11 at 12:01
add comment

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.