WordPress Custom post page with pagination

I have custom post types called clients, need to display 5 clients per page with pagination. The page what i have is page-clients.php

I have used the wp_pagenavi plugin.

Read More

I get a perfect navigation list 1,2,3 etc etc but on clicking them takes me to page not found

My Code

$args = array(
  'posts_per_page' => 5,
  'post_type' => 'clients',
  'paged' => get_query_var('page')

);

query_posts($args); 

<?php while ( have_posts() ) : the_post(); ?>
.....
<?php endwhile; // end of the loop. ?>

<?php wp_pagenavi(); ?> 
<?php wp_reset_query();?>

Related posts

Leave a Reply

2 comments

  1. Heres the way you can do it without pagination plugin 🙂 using WP_QUERY instead of query_posts

    $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; // setup pagination
    
        $the_query = new WP_Query( array( 
            'post_type' => 'clients',
            'paged' => $paged,
            'posts_per_page' => 5) 
        );
    
        while ( $the_query->have_posts() ) : $the_query->the_post();
            echo '<div>' . get_the_title() . '</div>';
                  the_content();
        endwhile;
    
    
        echo '<nav>';
        echo  '<div>'.get_next_posts_link('Older', $the_query->max_num_pages).'</div>'; //Older Link using max_num_pages
        echo  '<div>'.get_previous_posts_link('Newer', $the_query->max_num_pages).'</div>'; //Newer Link using max_num_pages
        echo "</nav>";
    
    
        wp_reset_postdata(); // Rest Data
    
  2. Pagination Like : Prev 1 2 3 Next

    <?php 
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
    $data= new WP_Query(array(
        'post_type'=>’YOUR_POST_TYPE’, // your post type name
        'posts_per_page' => 5, // post per page
        'paged' => $paged,
    ));
    
    if($data->have_posts()) :
        while($data->have_posts())  : $data->the_post();
                // Your code
        endwhile;
    
        $total_pages = $data->max_num_pages;
    
        if ($total_pages > 1){
    
            $current_page = max(1, get_query_var('paged'));
    
            echo paginate_links(array(
                'base' => get_pagenum_link(1) . '%_%',
                'format' => '/page/%#%',
                'current' => $current_page,
                'total' => $total_pages,
                'prev_text'    => __('« prev'),
                'next_text'    => __('next »'),
            ));
        }
        ?>    
    <?php else :?>
    <h3><?php _e('404 Error: Not Found', ''); ?></h3>
    <?php endif; ?>
    <?php wp_reset_postdata();?>
    

    Would you please try above code?