Last active
April 15, 2020 11:33
-
-
Save shaddam/4a8a6f18877bbc271dbdfe737b7281d4 to your computer and use it in GitHub Desktop.
How to add pagination in a Wordpress Custom Post Type Query
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // WP Query | |
| $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; | |
| $args = array( | |
| 'post_type' => !empty( $settings['grid_post_type'] ) ? $settings['grid_post_type'] : 'post', | |
| 'post_status' => 'publish', | |
| 'ignore_sticky_posts' => 1, | |
| 'posts_per_page' => !empty( $settings['post_limit'] ) ? $settings['post_limit'] : 3, | |
| 'order' => $postorder, | |
| 'paged' => $paged, | |
| ); | |
| // Use this code after end while | |
| <div class="col-12 clearfix"> | |
| <?php | |
| $total_pages = $grid_post->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, | |
| 'type' => 'list', | |
| 'prev_text' => __('<i class="fa fa-angle-left"></i> prev'), | |
| 'next_text' => __('next <i class="fa fa-angle-right"></i>'), | |
| )); | |
| } | |
| wp_reset_postdata(); | |
| wp_reset_query(); | |
| ?> | |
| </div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment