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.


1 Answer 1


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; ?>
  • 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!
    – Sampson
    Nov 3, 2010 at 20:55

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