I currently use this snippet to retrieve a list of the latest 5 (approved) comments:
<h2>Latest comments</h2>
<?php $comments = get_comments('status=approve&number=5'); ?>
<ul style="font-size:5px">
<?php foreach ($comments as $comment) { ?>
<li style="font-size:10px">
<div style="float:left;margin-right:3px"><?php echo get_avatar( $comment, '35' ); ?></div>
<em style="font-size:12px"><?php echo strip_tags($comment->comment_author); ?></em> (<a href="<?php echo get_option('home'); ?>/?p=<?php echo($comment->comment_post_ID); ?>/#comment-<?php echo($comment->comment_ID); ?>">link</a>)<br>
<?php echo wp_html_excerpt( $comment->comment_content, 35 ); ?>...
</li>
<?php } ?>
</ul>
It works, the problem is that this line is really dirty:
<a href="<?php echo get_option('home'); ?>/?p=<?php echo($comment->comment_post_ID); ?>/#comment-<?php echo($comment->comment_ID); ?>">link</a>
I use /year/month/day/ permalink structure and with the line above I get an extra redirect from ?p=POST_ID to that. I'm pretty sure there a better way to get comments permalink..
Can you help me? :)
You can use the get_comment_link() function. If you pass in the ID of the comment it will generate the http url to link to the comment.
ex:
get_comment_link($comment->comment_ID);
If $comment is a WP comment object you should be able to pass that in instead of the ID and it saves a lookup on the object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With