Last active
March 4, 2021 17:44
-
-
Save raazon/04605b73c747266221c29a0f061f1bc5 to your computer and use it in GitHub Desktop.
Wordpress custom post type pagination
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
<?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; |
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
<?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