Yes, I have got the following code, working like charm. I hope it will also help others who are looking for the same feature to display all the comments on one page with pagination.
Here is the code, enjoy!:
<?php
/*
Template Name: Display All Comments
Template Post Type: page
*/
?>
<?php
/*Set how many comments per page*/
$comments_per_page=4;
/*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 );
/*Get Comments*/
/*If you are going to get comments from a post you need to add 'post_id' => $post->ID, to the array*/
$comments = get_comments(array(
'status' => 'approve',
'number' => $comments_per_page,
'offset' => $offset
));
/*Show Comments*/
if ( $comments ) {
foreach ( $comments as $comment ) {
echo '<p>' . $comment->comment_content . '</p><br><br>';
}
} else {
// echo 'No comments found.';
}
/*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
));
?>
Thanks to theidioms.com
-
This reply was modified 4 years, 2 months ago by
laddi.
-
This reply was modified 4 years, 2 months ago by
laddi.
-
This reply was modified 4 years, 2 months ago by
laddi.
-
This reply was modified 4 years, 2 months ago by
laddi.