0

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>
<ul>
<?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; ?>
      </a>
  </li>
<?php endforeach; ?>
</ul>

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

3

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; ?>
</a>
  • 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 fuxia May 4 '16 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.