简体   繁体   中英

pagination in page with custom code in wordpress

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

My code:

<?php
<ol class="comment-list">

<?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.

Try this:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<ol class="comment-list">
    <?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; ?>

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM