This repository has been archived on 2020-05-08. You can view files and clone it, but cannot push or open issues or pull requests.
understrap/inc/pagination.php

94 lines
2.8 KiB
PHP
Raw Normal View History

2016-11-23 09:42:53 +00:00
<?php
/**
* Pagination layout.
*
* @package understrap
*/
/**
* Custom Pagination with numbers
* Credits to http://www.wpbeginner.com/wp-themes/how-to-add-numeric-pagination-in-your-wordpress-theme/
*/
if ( ! function_exists( 'understrap_pagination' ) ) :
function understrap_pagination() {
if ( is_singular() ) {
return;
}
global $wp_query;
/** Stop execution if there's only 1 page */
if ( $wp_query->max_num_pages <= 1 ) {
return;
}
$paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
$max = intval( $wp_query->max_num_pages );
/** Add current page to the array */
if ( $paged >= 1 ) {
$links[] = $paged;
}
/** Add the pages around the current page to the array */
if ( $paged >= 3 ) {
$links[] = $paged - 1;
$links[] = $paged - 2;
}
if ( ( $paged + 2 ) <= $max ) {
$links[] = $paged + 2;
$links[] = $paged + 1;
}
echo '<nav aria-label="Page navigation"><ul class="pagination ">' . "\n";
/** Link to first page, plus ellipses if necessary */
if ( ! in_array( 1, $links ) ) {
$class = 1 == $paged ? ' class="active page-item"' : ' class="page-item"';
printf( '<li %s><a class="page-link" href="%s"><i class="fa fa-step-backward" aria-hidden="true"></i></a></li>' . "\n",
$class, esc_url( get_pagenum_link( 1 ) ), '1' );
/** Previous Post Link */
if ( get_previous_posts_link() ) {
printf( '<li class="page-item"><span class="page-link">%1$s</span></li> ' . "\n",
get_previous_posts_link( '<span aria-hidden="true">&laquo;</span><span class="sr-only">Previous page</span>' ) );
}
if ( ! in_array( 2, $links ) ) {
echo '<li class="page-item"></li>';
}
}
// Link to current page, plus 2 pages in either direction if necessary.
sort( $links );
foreach ( (array) $links as $link ) {
$class = $paged == $link ? ' class="active page-item"' : ' class="page-item"';
printf( '<li %s><a href="%s" class="page-link">%s</a></li>' . "\n", $class,
esc_url( get_pagenum_link( $link ) ), $link );
}
// Next Post Link.
if ( get_next_posts_link() ) {
printf( '<li class="page-item"><span class="page-link">%s</span></li>' . "\n",
get_next_posts_link( '<span aria-hidden="true">&raquo;</span><span class="sr-only">Next page</span>' ) );
}
// Link to last page, plus ellipses if necessary.
if ( ! in_array( $max, $links ) ) {
if ( ! in_array( $max - 1, $links ) ) {
echo '<li class="page-item"></li>' . "\n";
}
$class = $paged == $max ? ' class="active "' : ' class="page-item"';
printf( '<li %s><a class="page-link" href="%s" aria-label="Next"><span aria-hidden="true"><i class="fa fa-step-forward" aria-hidden="true"></i></span><span class="sr-only">%s</span></a></li>' . "\n",
2017-01-27 13:26:46 +00:00
$class . '', esc_url( get_pagenum_link( esc_html( $max ) ) ), esc_html( $max ) );
2016-11-23 09:42:53 +00:00
}
echo '</ul></nav>' . "\n";
}
2017-01-27 13:26:46 +00:00
endif;