Last active
August 29, 2015 14:07
-
-
Save sixlive/d510b92b07f3822bf87c to your computer and use it in GitHub Desktop.
WordPress: Pagination With Get Post Awesome Loop
This file contains 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 | |
// Setup vars for pagination. | |
$per_page = 10; | |
$current_page = ( intval( get_query_var('paged') ) ) ? intval( get_query_var( 'paged' ) ) : 1; | |
$offset = $per_page * ($current_page - 1); | |
// Config, subsitute / add whatever you need. Leave per_page and offset as the vars. | |
$sweet_args = [ | |
'numberposts' => $per_page, | |
'post_type' => 'post', | |
'offset' => $offset | |
]; | |
// Args for total post should be the same except we want no offset and numberposts to be -1 so we get all of them. You could define some more variables to make this a litle more seamless. Hard to say without seeing your code. | |
$total_args = [ | |
'numberposts' => -1, | |
'post_type' => 'post', | |
]; | |
// Neet total articles for pagination | |
$total_posts = ceil(count( get_posts( $total_args ) )); | |
// Results to loop through | |
$results = get_post_awesome( $sweet_args ); | |
// Loop it out | |
foreach ( $results as $post ): setup_postdata( $post ); | |
// post stuff | |
endforeach; | |
// Dont foreget this | |
wp_reset_postdata( $post ); | |
// Pagination logic | |
// This could be moved above to loop if you feel like it. | |
$big = 999999999; // Need an unlikely integer | |
if ( $current_page < 5 ) | |
$mid = 4; | |
else | |
$mid = 2; | |
$pagination = paginate_links( array( | |
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ), | |
'format' => '?paged=%#%', | |
'current' => max( 1, $current_page ), | |
'total' => ceil( $total_posts / $per_page), | |
'type'=> 'array', | |
'end_size'=> 0, | |
'mid_size'=> $mid, | |
'prev_text' => __('Prev'), | |
'next_text' => __('Next'), | |
) ); | |
// Echo the pagination links | |
echo implode( ' ', $pagination ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment