Skip to content

Instantly share code, notes, and snippets.

@mahdi-alavi
Created October 18, 2018 18:09
Show Gist options
  • Save mahdi-alavi/f41ead3ba3b7353a8d5b84a44b873825 to your computer and use it in GitHub Desktop.
Save mahdi-alavi/f41ead3ba3b7353a8d5b84a44b873825 to your computer and use it in GitHub Desktop.
wordpress: Bootstrap 4.1 pagination
/* Bootstrap pagination.
=========================================================================== */
function itl_paging_nav() {
global $wp_query, $wp_rewrite;
// Setting up default values based on the current URL.
$pagenum_link = html_entity_decode( get_pagenum_link() );
$url_parts = explode( '?', $pagenum_link );
// Get max pages and current page out of the current query, if available.
$total = isset( $wp_query->max_num_pages ) ? $wp_query->max_num_pages : 1;
$current = get_query_var( 'paged' ) ? intval( get_query_var( 'paged' ) ) : 1;
// Append the format placeholder to the base URL.
$pagenum_link = trailingslashit( $url_parts[0] ) . '%_%';
// URL base depends on permalink settings.
$format = $wp_rewrite->using_index_permalinks() && ! strpos( $pagenum_link, 'index.php' ) ? 'index.php/' : '';
$format .= $wp_rewrite->using_permalinks() ? user_trailingslashit( $wp_rewrite->pagination_base . '/%#%', 'paged' ) : '?paged=%#%';
$args = array(
'base' => $pagenum_link,
'format' => $format,
'total' => $total,
'current' => $current,
'prev_text' => __( '« Previous' ),
'next_text' => __( 'Next »' ),
'end_size' => 1,
'mid_size' => 2,
'type' => 'array',
'add_args' => false,
);
$pages = paginate_links( $args );
if ( ! $pages ) {
return;
}
if ( $total < 2 ) {
return;
}
$pagination = '<div class="pagination"><ul class="pagination">';
foreach ( $pages as $page ) {
$pagination .= sprintf( '<li class="page-item %s">%s</li>',
strpos ( $page, 'current' ) !== false ? 'active' : '',
str_replace( 'page-numbers', 'page-link', $page )
);
}
$pagination .= '</ul></div>';
echo $pagination;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment