How to add pagination to comments written by custom code

Hi I have retrieved comments by custom code in my template file as the following

<?php $comments = get_comments();?>
<?php foreach($comments as $comment) : ?>
<?php if ($comment->comment_approved == '0') : ?>
<p class="waiting-message">Your comment is awaiting moderation.</p>
<?php endif; ?>
<?php echo $comment->comment_author; ?>
<?php echo comment_date('n M y'); ?>
<?php echo $comment->comment_content;?>
<?php endforeach; ?>

Now I don’t know how to do numbered pagination like << 1, 2, 3 >>… Please help me

Related posts

Leave a Reply

3 comments

  1. define('DEFAULT_COMMENTS_PER_PAGE',5);
    
    $id=get_the_ID();
    
    $page = (get_query_var('page')) ? get_query_var('page') : 1;;
    
    //$page=2;
    
    
    
    
    
    $limit = DEFAULT_COMMENTS_PER_PAGE;
    
    $offset = ($page * $limit) - $limit;
    
    $param = array(
    
        'status'=>'approve',
    
        'offset'=>$offset,
    
        'post_id'=>$id,
    
        'number'=>$limit,
    
    );
    $total_comments = get_comments(array('orderby' => 'post_date' ,
    
                'order' => 'DESC',
    
                'post_id'=>$id,
    
               'status' => 'approve',
    
                'parent'=>0));
    
    $pages = ceil(count($total_comments)/DEFAULT_COMMENTS_PER_PAGE);
    $comments = get_comments($param );
    

    your comment will be like this

    and pagination like

    <?php
    
            $args = array(
    
    'base'         => @add_query_arg('page','%#%'),
    
    'format'       => '?page=%#%',
    
    'total'        => $pages,
    
    'current'      => $page,
    
    'show_all'     => False,
    
    'end_size'     => 1,
    
    'mid_size'     => 2,
    
    'prev_next'    => True,
    
    'prev_text'    => __('Previous'),
    
    'next_text'    => __('Next'),
    
    'type'         => 'plain');
    
    // ECHO THE PAGENATION 
    
    echo paginate_links( $args );
    
    
    
    ?>
    
  2. I was trying to accomplish something similar to the original poster: gather all the comments on a WordPress site and display them in a paginated layout.

    The above answer was very helpful, but I could never get it to completely work on my site. I tried a little different approach and it works for me. I’m posting the code here in case it’s helpful to others.

            <?php
            //Page variables.
            $page = (get_query_var('paged')) ? get_query_var('paged') : 1;
            $per_page = 10;
            $offset = ( ($page -1) * $per_page);
    
            //Args - comments (paginated).
            $args1 = array(
                'status' => 'approve',
                'post_status' => 'publish',
                'number' => $per_page,
                'offset' => $offset,
            );
    
            //Args - comments (not paginated).
            $args2 = array(
                'status' => 'approve',
                'post_status' => 'publish',
            );
    
            //Get comments (paginated).
            $all_comments1 = get_comments( $args1 );
    
            //Get comments (not paginated) and count.
            $all_comments2 = get_comments( $args2 );
            $all_comments_num = count( $all_comments2 );
    
            //Display the paginated comments.
            if ( $all_comments1 ) {
                foreach ( (array) $all_comments1 as $comment ) {
                    echo '<section class="news-post"><header><time>' . get_comment_date('n.j.y') . '</time><h5>' . $comment->comment_author . ' on <a href="' . esc_url( get_comment_link( $comment ) ) . '">' . get_the_title( $comment->comment_post_ID ). '</a>:</h5>' . $comment->comment_content . '</section>';
                }
            }
    
            //Args - "paginate_links".
            $page_args = array(
                'base'         => get_permalink( get_the_ID() ). '%_%',
                'format'       => 'page%#%',
                'total'        => ceil($all_comments_num / $per_page),
                'current'      => $page,
                'show_all'     => True,
                'end_size'     => 2,
                'mid_size'     => 2,
                'prev_next'    => True,
                'prev_text'    => __('« Previous'),
                'next_text'    => __('Next »'),
                'type'         => 'plain',
            );
    
            //Display the "paginate_links".
            echo paginate_links($page_args);
            ?>
    
  3. Just in case someone’s in my situation:

    I was looking to do exactly what the OP was trying to achieve, but on a product single page in Woocommerce: nothing worked. One of the main issues was that, for some reason, Woocommerce will redirect to the original URL if a public query variable is used. So, starting from @anstrangel0ver’s solution, here’s what I did:

    Since using ‘page’ as a query var was out of the question, I added one via my functions.php file:

    function themeslug_query_vars( $qvars ) {
        $qvars[] = 'review';
        return $qvars;
    }
    add_filter( 'query_vars', 'themeslug_query_vars' );
    

    I then slightly modified his code, pasted on my product page:

    $ID = $product->get_ID();
    
    $page = (get_query_var('review')) ? get_query_var('review') : 1;
    
    $total_comments = get_comments(array(
        'orderby'   => 'post_date',
        'order'     => 'DESC',
        'post_id'   => $ID,
        'status'    => 'approve',
        'parent'    => 0,
    ));
    
    $per_page = 4;
    $pages = ceil(count($total_comments)/$per_page);
    
    $limit = $per_page;
    $offset = ($page * $limit) - $limit;
    
    $param = array(
        'status'    => 'approve',
        'offset'    => $offset,
        'post_id'   => $ID,
        'number'    => $limit,
    );
    
    // Pagination args
    $args = array(
        'base'         => get_permalink($ID). '%_%',
        // 'format'       => 'comment-page-%#%',
        'format'       => '?review=%#%',
        'total'        => $pages,
        'current'      => $page,
        'show_all'     => false,
        'end_size'     => 1,
        'mid_size'     => 2,
        'prev_next'    => true,
        'prev_text'    => __('<'),
        'next_text'    => __('>'),
        'type'         => 'plain',
    );
    
    $comments = get_comments($param);
    

    And then a foreach to create my custom structure:

    <?php foreach ($comments => $comment): ?>
        //your code
    <?php endforeach; ?>