Skip to content

Instantly share code, notes, and snippets.

@raazon
Last active March 4, 2021 17:44
Show Gist options
  • Save raazon/04605b73c747266221c29a0f061f1bc5 to your computer and use it in GitHub Desktop.
Save raazon/04605b73c747266221c29a0f061f1bc5 to your computer and use it in GitHub Desktop.
Wordpress custom post type pagination
<?php
/*
* Custom Post Pagination
* @since 1.0.0
* return
*/
if (!function_exists('wpdocs_custom_posts_pagination')) :
function wpdocs_custom_posts_pagination($the_query=NULL, $paged=1){
global $wp_query;
$the_query = !empty($the_query) ? $the_query : $wp_query;
if ($the_query->max_num_pages > 1) {
$big = 999999999; // need an unlikely integer
$items = paginate_links(apply_filters('wpdocs_posts_pagination_paginate_links', array(
'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
'format' => '?paged=%#%',
'prev_next' => TRUE,
'current' => max(1, $paged),
'total' => $the_query->max_num_pages,
'type' => 'array',
'prev_text' => ' <i class="fas fa-angle-double-left"></i> ',
'next_text' => ' <i class="fas fa-angle-double-right"></i> ',
'end_size' => 1,
'mid_size' => 1
)));
$pagination = "<div class=\"col-sm-12 text-center\"><div class=\"ic-pagination\"><ul><li>";
$pagination .= join("</li><li>", (array)$items);
$pagination .= "</li></ul></div></div>";
echo apply_filters('wpdocs_posts_pagination', $pagination, $items, $the_query);
}
}
endif;
<?php
if ( get_query_var( 'paged' ) ) {
$paged = get_query_var( 'paged' );
} elseif ( get_query_var( 'page' ) ) {
$paged = get_query_var( 'page' );
} else {
$paged = 1;
}
$args = array(
'post_type' => 'post_type_here',
'posts_per_page' => 10,
'paged' => $paged,
);
// the query
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php wpdocs_custom_posts_pagination($the_query, $paged); ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php esc_html_e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment