Show latest comments on all pages in WordPress

2Fast2BCn

New member
Joined
May 27, 2016
Messages
10
Points
0
Normally WordPress allows us to display the comments on a single article (post) through comments.php file included in the template. However, you can manually edit to be able to display the comments from visitors on all pages (archive, category, search, ...) on your blog or any location on the site if wanted.

Just copy the following codes and paste it into any page or location that you want to show on.

Code:
<?php // display latest comments for each post on non-single page views
$comment_array = array_reverse(get_approved_comments($wp_query->post->ID));
$count = 1; // number of comments
if ($comment_array) { ?>
 
    <h3><?php comments_number('No comment', '1 comment', '% comments'); ?></h3>
    <ul>
    <?php foreach ($comment_array as $comment) {
        if ($count++ <= 2) { ?>
        <li><?php comment_author_link(); ?>: <?php comment_excerpt(); ?></li>
        <?php }
    } ?>
    </ul>
 
<?php } else {
    echo '<p>No comments yet.</p>';
} ?>
Hope it helps and I am looking forward to seeing other ways from all you guys here.
 
Latest threads
Recommended threads
Similar threads
Top