Last active
January 25, 2022 14:24
-
-
Save josanua/09dfa6f8e53a58288250463c5db3c87d to your computer and use it in GitHub Desktop.
bs4pagination
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 | |
/** | |
* @param WP_Query|null $wp_query | |
* @param bool $echo | |
* | |
* @return string | |
* Accepts a WP_Query instance to build pagination (for custom wp_query()), | |
* or nothing to use the current global $wp_query (eg: taxonomy term page) | |
* - Tested on WP 4.9.5 | |
* - Tested with Bootstrap 4.1 | |
* - Tested on Sage 9 | |
* | |
* USAGE: | |
* <?php echo bootstrap_pagination(); ?> //uses global $wp_query | |
* or with custom WP_Query(): | |
* <?php | |
* $query = new \WP_Query($args); | |
* ... while(have_posts()), $query->posts stuff ... | |
* echo bootstrap_pagination($query); | |
* ?> | |
* https://gist.github.com/mtx-z/f95af6cc6fb562eb1a1540ca715ed928 | |
*/ | |
function bootstrap_pagination( \WP_Query $wp_query = null, $echo = true ) { | |
if ( null === $wp_query ) { | |
global $wp_query; | |
} | |
$pages = paginate_links( [ | |
'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ), | |
'format' => '?paged=%#%', | |
'current' => max( 1, get_query_var( 'paged' ) ), | |
'total' => $wp_query->max_num_pages, | |
'type' => 'array', | |
'show_all' => false, | |
'end_size' => 3, | |
'mid_size' => 1, | |
'prev_next' => true, | |
// 'prev_text' => __( '<' ), | |
// 'next_text' => __( '>' ), | |
'prev_text' => __('«'), | |
'next_text' => __('»'), | |
'add_args' => false, | |
'add_fragment' => '', | |
] | |
); | |
if ( is_array( $pages ) ) { | |
//$paged = ( get_query_var( 'paged' ) == 0 ) ? 1 : get_query_var( 'paged' ); | |
$pagination = '<nav aria-label="Page navigation" class="w-100 d-flex align-items-center justify-content-center mb-4"><ul class="pagination">'; | |
foreach ( $pages as $page ) { | |
$pagination .= '<li class="page-item"> ' . str_replace( 'page-numbers', 'page-link', $page ) . '</li>'; | |
} | |
$pagination .= '</ul> </nav>'; | |
if ( $echo ) { | |
echo $pagination; | |
} else { | |
return $pagination; | |
} | |
} | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment