Display latest comments for each post in WordPress

Here is the quick WordPress function to fetch latest approved comments for each posts, Normally, WordPress displays comments on single post views via the comments.php => theme template file. But you can also easily display comments for each post on the landing page and other pages where you want to display just use following php code.

 
$commentArr = array_reverse(get_approved_comments($wp_query->post->ID));
$count = 1; // number of comments
if ($commentArr) { ?>
	

commentsNum('0 comment', '1 comment', '% comments'); ?>

    foreach ($commentArr as $comment) { if ($count++ <= 2) { ?>
  • comment_author_link(); ?>: comment_excerpt(); ?>
  • } } ?>
} else { echo '

No Comments..

'
; } ?>

No Comments..’;
} ?>

Leave a Reply

Your email address will not be published. Required fields are marked *

Top