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.

<?php 
$commentArr = array_reverse(get_approved_comments($wp_query->post->ID));
$count = 1; // number of comments
if ($commentArr) { ?>
	<h3><?php commentsNum('0 comment', '1 comment', '% comments'); ?></h3>
	<ul>
	<?php foreach ($commentArr as $comment) {
		if ($count++ <= 2) { ?>
		<li><?php comment_author_link(); ?>: <?php comment_excerpt(); ?></li>
		<?php }
	} ?>
	</ul>
<?php } else {
	echo '<p>No Comments..</p>';
} ?>



One Reply to “Display latest comments for each post in WordPress”

  1. Thanks Sir for your mail.I have read all mail from your hand.Thanks for your support.Always you will connect with me.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.