0

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!

2 Answers 2

2

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>
2
  • 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
    Commented Sep 16, 2011 at 10:52
  • updated my answer Commented Sep 16, 2011 at 11:48
0

You should probably use this: get_comment_link()... and do something like this in your href value:

<?php echo get_comment_link($comment->comment_ID); ?>

Because if you use pagination for your comments, this can automatically determine the page the comment is in as well.

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