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've got to update a bit of sloppy code that shows the 5 most recent comments across the entire blog. The code is using WordPress' get_comments() method, which doesn't appear to return comment id's, or comment links (direct links to comments within posts via the hash-tag).

How can I collect more information about the particular comments, and make them links to the actual comments within their parent pages?

<h3>Recent Comments</h3>
<?php $comments = get_comments(array('status'=>'approve','number'=>'5')); ?>
<?php foreach ($comments as $comment): ?>
  <?php $post = get_post($comment->comment_post_ID, 'OBJECT'); ?>
  <li><?php print $comment->comment_author; ?> on 
      <a href="<?php print get_permalink($post->post_ID); ?>">
        <?php print $post->post_title; ?>
<?php endforeach; ?>

This is located on the front page of the site, outside of the loop.

share|improve this question
You should probably use get_comment_link()... See my answer to a similar question here. –  gmaggio Jan 15 at 22:23

1 Answer 1

up vote 3 down vote accepted

You're really close! Add #comment-<?php comment_id(); ?>to the href to append the anchor link

<a href="<?php echo get_permalink($comment->comment_post_ID);?>#comment-<?php comment_ID() ?>">
     <?php echo $post->post_title; ?>
share|improve this answer
I had saw that function but thought (wrongly) that it would only work within a real loop, and not necessarily a call to get_comments(). Thanks for clearing that up for me! –  Jonathan Sampson Nov 3 '10 at 20:55

Your Answer


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.