Skip to content

Instantly share code, notes, and snippets.

@milanarandjelovic
Last active January 11, 2021 11:50
Show Gist options
  • Save milanarandjelovic/8654f7ec652b4bcf2e85d097a32138e5 to your computer and use it in GitHub Desktop.
Save milanarandjelovic/8654f7ec652b4bcf2e85d097a32138e5 to your computer and use it in GitHub Desktop.
WordPress - Bootstrap 4 pagination
<?php
/**
* Template for displaying bootstrap 4 post pagination.
*
* @package WordPress
* @subpackage Bootstrap
* @author Milan Arandjelovic
*/
if ( ! function_exists( 'bootstrap_pagination' ) ) {
/**
* Return HTML for bootstrap 4 pagination.
*
* @since 1.0.0
* @param array $args Array of arguments.
*/
function bootstrap_pagination( $args = array() ) {
global $wp_query;
$defaults = array(
'range' => 4,
'custom_query' => false,
'previous_string' => __( 'Previous', 'bootstrap' ),
'next_string' => __( 'Next', 'bootstrap' ),
'before_output' => '<nav><ul class="pagination">',
'after_output' => '</ul></nav>',
);
$args = wp_parse_args(
$args,
apply_filters( 'bootstrap_pagination_defaults', $defaults )
);
$args['range'] = (int) $args['range'] - 1;
if ( ! $args['custom_query'] ) {
$args['custom_query'] = $wp_query;
}
$count = (int) $args['custom_query']->max_num_pages;
$page = intval( get_query_var( 'paged' ) );
$ceil = ceil( $args['range'] / 2 );
if ( $count <= 1 ) {
return false;
}
if ( ! $page ) {
$page = 1;
}
if ( $count > $args['range'] ) {
if ( $page <= $args['range'] ) {
$min = 1;
$max = $args['range'] + 1;
} elseif ( $page >= ( $count - $ceil ) ) {
$min = $count - $args['range'];
$max = $count;
} elseif ( $page >= $args['range'] && $page < ( $count - $ceil ) ) {
$min = $page - $ceil;
$max = $page + $ceil;
}
} else {
$min = 1;
$max = $count;
}
$pagination = '';
$previous = intval( $page ) - 1;
$previous = esc_attr( get_pagenum_link( $previous ) );
$first_page = get_pagenum_link( 1 );
if ( $first_page && ( 1 !== $page ) ) {
$pagination .= '<li class="page-item"><a href="' . $first_page . '" class="page-link">' . __( '<i class="fa fa-angle-double-left"></i>', 'bootstrap' ) . '</a></li>';
}
if ( $previous && ( 1 !== $page ) ) {
$pagination .= '<li class="page-item"><a href="' . $previous . '" class="page-link" title="' . __( 'previous', 'bootstrap' ) . '">' . $args['previous_string'] . '</a></li>';
}
if ( ! empty( $min ) && ! empty( $max ) ) {
for ( $i = $min; $i < $max; $i++ ) {
if ( $page === $i ) {
// Active class.
$pagination .= '<li class="page-item active"><a class="page-link">' . str_pad( (int) $i, 1, '0', STR_PAD_LEFT ) . '</a></li>';
} else {
$pagination .= sprintf( '<li class="page-item"><a href="%s" class="page-link">%d</a></li>', esc_attr( get_pagenum_link( $i ) ), $i );
}
}
}
$next = intval( $page ) + 1;
$next = esc_attr( get_pagenum_link( $next ) );
if ( $next && ( $count !== $page ) ) {
$pagination .= '<li class="page-item"><a href="' . $next . '" class="page-link" title="' . __( 'next', 'bootstrap' ) . '">' . $args['next_string'] . '</a></li>';
}
$last_page = esc_attr( get_pagenum_link( $count ) );
if ( $last_page && ( $count !== $page ) ) {
$pagination .= '<li class="page-item"><a href="' . $last_page . '" class="page-link">' . __( '<i class="fa fa-angle-double-right"></i>', 'phoenix' ) . '</a></li>';
}
if ( $last_page && ( $count === $page ) ) {
$pagination .= '<li class="page-item active"><a href="' . $last_page . '" class="page-link">' . $count . '</a></li>';
}
if ( isset( $pagination ) ) {
echo $args['before_output'] . $pagination . $args['after_output'];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment