How to add pagination in WP CPT

Simply add this function after ending while loop.

if (function_exists("cpt_pagination")) {            
  cpt_pagination($query);                  
}

Also, add this function into the functions.php file.

function cpt_pagination($query){
    global $loop;
    $query = $query ? $query : $loop;
    $big = 999999999;

    $paginate = paginate_links( array(
      'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
      'type' => 'array',
      'total' => $query->max_num_pages,
      'format' => '?paged=%#%',
      'current' => max( 1, get_query_var('paged') ),
    //   'prev_previous'  =>('← Older posts'),
    //   'prev_next'          => ( 'Newer posts →' ),
    //   'prev_text' => __('«'),
      'prev_text' => __(''),
    //   'next_text' => __('»'),
      'next_text' => __(''),
    ));

    if ($query->max_num_pages > 1) :
        
  ?>
        <div class="paginate">
          <ul class="pagination">
             <?php
                foreach ( $paginate as $page ) {
                   echo '<li>' . $page . '</li>';
                }
             ?>
           </ul>
         </div>
<?php
    endif; 
}

Leave a Reply

Your email address will not be published.