WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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. – Giraldi Jan 15 '15 at 22:23
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! – Sampson Nov 3 '10 at 20:55

protected by toscho May 4 at 18:52

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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