Home > Blockchain >  pagination in page with custom code in wordpress
pagination in page with custom code in wordpress

Time:01-19

How to add pagination for comments in page with custom code in Wordpress?

My code:

<?php
<ol >

<?php   
    
$comments = get_comments(array(
'number'  => '8',
'status' => 'approve',
'offset' => $offset
));

foreach( $comments as $comment ){
    echo $comment->comment_author . '<br />' . $comment->comment_content;
}
                        
?>          
    
</ol>

<?php paginate_comments_links ( array( 'prev_text'  =>  '« PREV' ,  'next_text'  =>  'NEXT »' ) ); ?>

I searched here for solutions, but did not find any. There are old posts from before 2014. Other codes given here do not work. I have been struggling for several hours.

Please help.

CodePudding user response:

Try this :

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<ol >
    <?php
    $comments_per_page = 8;
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $offset = ($paged - 1) * $comments_per_page;
    $args = array(
        'number'  => $comments_per_page,
        'status' => 'approve',
        'offset' => $offset
    );
    wp_list_comments($args);
    ?>
</ol>
<?php paginate_comments_links ( array( 'prev_text'  =>  '« PREV' ,  'next_text'  =>  'NEXT »' ) ); ?>

<?php endwhile; endif; ?>

CodePudding user response:

How are you setting the $offset variable? Try something like this, code is not tested but it will give you idea to get comments pagination working.

<?php
  $current_page = get_query_var( 'cpage' ) ? get_query_var( 'cpage' ) :  1;
  $comments_per_page = 8;
  $offset = ( $current_page - 1 ) * $comments_per_page;
  • Related