Search code examples
phpwordpresspaginationcustom-post-type

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.


Solution

  • 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();