If you want to display all comments on one page in WordPress, you can use the code snippet below, which display recent comments with a pagination. I created a page template and added the code snippet in it.
Display all comments on one page in WordPress
It's easy to use a WordPress plugin to show all comments in any place you want. There is a plugin named "Show All Comments" for this purpose. You might want to try it.
I referred to WordPress Get Recent Comments to create a page to display all comments from a WP site.
First, please add the following function to your theme's function file. (Please create a child theme first if it's not installed and activated.)
// truncate string at word
function shapeSpace_truncate_string($phrase, $max_words) {
$phrase_array = explode(' ', $phrase);
if (count($phrase_array) > $max_words && $max_words > 0)
$phrase = implode(' ', array_slice($phrase_array, 0, $max_words)) . __('...', 'shapespace');
return $phrase;
}
I created a page template and add the following code snippet:
<!-- Start of
End of the List of Recent Comments with a pagination -->
<div class="recent-comments-widget">
<ul>
<?php
/*Set how many comments per page*/
$comments_per_page=30;
/*Count comments and count only approved*/
$all_comments = wp_count_comments();
/*If you are going to get only comments from a post you need to change the above code to
$all_comments = wp_count_comments($post->ID);
*/
$all_comments_approved = $all_comments->approved;
/*Get Current Page Var*/
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
/*How many comments offset*/
$offset = (($paged-1) * $comments_per_page) ;
/*Max number of pages*/
$max_num_pages = ceil( $all_comments_approved / $comments_per_page );
$args_comments = array('number' => $comments_per_page, 'offset' => $offset, 'status' => 'approve');
$recent_comments = get_comments($args_comments);
foreach ($recent_comments as $r) {
echo '<li><span style="font-weight: bold;">' . $r->comment_author . '</span> at <b><a href="'. get_comment_link($r->comment_ID) .'" title="'. __('Date/time:', 'shapespace') . $r->comment_date .'">';
echo get_the_title($r->comment_post_ID) .'</a></b>: ';
echo shapeSpace_truncate_string(get_comment_excerpt($r->comment_ID), 12) .'</li>';
}
?>
</ul>
<div class="comments-pg">
<?php
/*Set current page for pagination*/
$current_page = max(1, get_query_var('paged'));
/*Echo paginate links*/
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'current' => $current_page,
'total' => $max_num_pages,
'prev_text' => __('« Previous'),
'next_text' => __('Next »'),
'end_size' => 2,
'mid-size' => 3
)); ?>
</div>
</div><!-- End of the List of Recent Comments with a pagination -->
Please change the value of $comments_per_page as you wish. If you set it to "20", 20 comments will be displayed each page.
For example, in the GeneratePress theme, I made a file named "page-comments.php" with the following codes under the child theme's directory. (Please replace "// Your custom code here" with the code snippet above.)
<?php
/**
* Template Name: Recent Comments
*
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
get_header(); ?>
<div <?php generate_do_attr( 'content' ); ?>>
<main <?php generate_do_attr( 'main' ); ?>>
<?php
/**
* generate_before_main_content hook.
*
* @since 0.1
*/
do_action( 'generate_before_main_content' );
if ( generate_has_default_loop() ) {
while ( have_posts() ) :
the_post();
generate_do_template_part( 'page' );
endwhile;
}
/**
* generate_after_main_content hook.
*
* @since 0.1
*/
do_action( 'generate_after_main_content' );
?>
// Your custom code here
</main>
</div>
<?php
/**
* generate_after_primary_content_area hook.
*
* @since 2.0
*/
do_action( 'generate_after_primary_content_area' );
generate_construct_sidebars();
get_footer();
Now you can create a page and choose the "Recent Comments" template from the Template section:
You can check a working page.