How to include pagination in a WordPress Custom Post Type

How to include pagination in a Wordpress Custom Post Type

Now that you have a custom query for your CPT, you can loop through the results and display them on your WordPress page template:

<?php

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $data= new WP_Query(array(
        'post_type'=>'post',
        'posts_per_page' => 9,
        //'offset' => 1,
        'paged' => $paged,
    ));
    if($data->have_posts()) :
        ?>
        <div class="vc_row wpb_row vc_row-fluid">
            <?php         
                while($data->have_posts())  : $data->the_post();
                    ?>
                        <div class="wpb_column vc_column_container vc_col-sm-4">
                            <div class="vc_column-inner">
                                <div class="wpb_wrapper">
                                    <div class="blog-section-3-sub-1">
                                        <img src="<?php the_post_thumbnail_url('full'); ?>" class="img-responsive" alt="<?php the_title(); ?>">
                                        <div class="blog-section-3-sub-2">
                                            <h2><?php the_title(); ?></h2>
                                            <p><?php echo substr(get_the_excerpt(), 0,80); ?></p>
                                            <a class="button" href="<?php echo get_permalink( $post->ID ); ?>">Read More</a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    <?php
                endwhile;
            ?>
        </div>

        <div class="">
            <div class="blog-section-3-sub-2-paginate">
                <?php
                $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'    => __('<i class="fa fa-angle-left"></i>'),
                        'next_text'    => __('<i class="fa fa-angle-right"></i>'),
                    ));
                }
                ?>  
            </div>  
        </div>  
    <?php else :?>
        <h2><?php _e('404 Error Not Found', ''); ?></h2>
    <?php endif; ?>

<?php wp_reset_postdata();  

?>
Learn Tutorials Point
We Well organized and easy-to-understand Interactive tutorials With lots of examples of how to use Tutorials WordPress, PHP, Bootstrap, Bootstrap 4, HTML, CSS, AJAX, jQuery, Woocommerce, Post, Database, Javascript, Theme, Responsive, Templates.

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact us

Fill in the form below or give us a call and we'll contact you. We endeavour to answer all enquiries within 24 hours on business days.