• Resolved laddi

    (@laddi)


    I want to display all the comments available on my website with pagination.
    Maximum 5/10 comments per page, with date, author and linked to respective posts.

    This is what I am currently trying to use:

    <?php
    /*
    Template Name: Display All Comments
    Template Post Type: page
    */
    ?>
    
    <?php wp_list_comments('per_page=5', get_comments()) ?> 
    <div class="navigation">
    <?php paginate_comments_links() ?>
    <?php previous_comments_link() ?>
    <?php next_comments_link() ?>
    </div>
    • This topic was modified 4 years, 2 months ago by laddi.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi

    You just need to check the box and choose the amount of comments :

    Admin > Settings > Discussion then in Other comment settings you check 6 text boxes

    Thread Starter laddi

    (@laddi)

    Thanks for the reply Marcio,
    But it’s not working.

    hi, did you solve the problem?

    Thread Starter laddi

    (@laddi)

    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' => __('&laquo; Previous'),
    'next_text' => __('Next &raquo;'),
    '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.

    I am happy to know that you have resolved ??

    Please don’t forget to mark the topic as resolved

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Page Template to Show All Comments with Pagination’ is closed to new replies.