Skip to content
Advertisement

How To Make Pagination For Custom Post Type In WordPress?

PRECONDITION:

  1. I created post type ‘comparison’.
  2. I created archive page for ‘comparison’ post type only.

TASK: I need to create pagination at archive page of ‘comparison’.

PROBLEM: I tried to use <?php echo paginate_links(); ?> but it doesn’t work, pls help.

Advertisement

Answer

Try the following code

$query = new WP_Query( 
    array(
        'posts_per_page'=>30,
        'post_type'=>'comparison',
        'paged' => get_query_var('paged') ? get_query_var('paged') : 1
    ) 
); 
?>
<?php while ($query -> have_posts()) : $query -> the_post(); ?>
//your code
endwhile;
$total_pages = $query->max_num_pages;
if ($total_pages > 1){
    $big = 999999999; // need an unlikely integer
    echo paginate_links( array(
        'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
        'format' => '?paged=%#%',
        'current' => max( 1, get_query_var('paged') ),
        'total' => $query->max_num_pages
    ) );
}
wp_reset_postdata();
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement