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
Replies
0
Views
93
Replies
0
Views
116
Replies
1
Views
156
Recommended threads
Replies
3
Views
4,240
Replies
10
Views
6,670
Replies
45
Views
21,349
Replies
11
Views
6,644

Referral contests

Referral link for :

Sponsors

Popular tags

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top