Skip to content

Instantly share code, notes, and snippets.

@corradomatt
Created November 27, 2013 21:46
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save corradomatt/7683816 to your computer and use it in GitHub Desktop.
Save corradomatt/7683816 to your computer and use it in GitHub Desktop.
Advanced Pagination WordPress Roots Theme
<?php
if ( !function_exists('your_pagination')) :
function your_pagination($custom_query) {
if ( !$current_page = get_query_var( 'paged' ) ) $current_page = 1;
$permalinks = get_option( 'permalink_structure' );
if( is_front_page() ) {
$format = empty( $permalinks ) ? '?paged=%#%' : 'page/%#%/';
} else {
$format = empty( $permalinks ) || is_search() ? '&paged=%#%' : 'page/%#%/';
}
$big = 999999999; // need an unlikely integer
$pagination = paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => $format,
'current' => $current_page,
'total' => $custom_query->max_num_pages,
'mid_size' => '4',
'type' => 'list',
'next_text' => __( 'Next' ),
'prev_text' => __( 'Previous' )
) );
$pagination = explode( "\n", $pagination );
$pagination_mod = array();
foreach ( $pagination as $item ) {
( preg_match( '/<ul class=\'page-numbers\'>/i', $item ) ) ? $item = str_replace( '<ul class=\'page-numbers\'>', '<ul class=\'pagination\'>', $item ) : $item;
( preg_match( '/class="prev/i', $item ) ) ? $item = str_replace( '<li', '<li class="pagination-prev"', $item ) : $item;
( preg_match( '/class="next/i', $item ) ) ? $item = str_replace( '<li', '<li class="pagination-next"', $item ) : $item;
( preg_match( '/page-numbers/i', $item ) ) ? $item = str_replace( 'page-numbers', 'page-numbers pagenav', $item ) : $item;
$pagination_mod[] .= $item;
}
?>
<div class="pagination">
<p class="counter">
<?php printf( __( 'Page %1$s of %2$s' ), $current_page, $custom_query->max_num_pages ); ?>
</p>
<?php foreach( $pagination_mod as $page ) {
echo $page;
} ?>
</div>
<?php }
endif;
@CatinhoCR
Copy link

Hey there, I notice this is quite old but was wondering if you still use it? Tried to use it myself but when I go to page 2 (for example), the page 1 is not clickable so Im not able to return to page 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment