|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * WordPress Bootstrap Pagination |
| 4 | + */ |
| 5 | + |
| 6 | +function wp_bootstrap_pagination( $args = array() ) { |
| 7 | + |
| 8 | + $defaults = array( |
| 9 | + 'range' => 4, |
| 10 | + 'custom_query' => FALSE, |
| 11 | + 'previous_string' => __( '<i class="glyphicon glyphicon-chevron-left"></i>', 'text-domain' ), |
| 12 | + 'next_string' => __( '<i class="glyphicon glyphicon-chevron-right"></i>', 'text-domain' ), |
| 13 | + 'before_output' => '<div class="post-nav"><ul class="pager">', |
| 14 | + 'after_output' => '</ul></div>' |
| 15 | + ); |
| 16 | + |
| 17 | + $args = wp_parse_args( |
| 18 | + $args, |
| 19 | + apply_filters( 'wp_bootstrap_pagination_defaults', $defaults ) |
| 20 | + ); |
| 21 | + |
| 22 | + $args['range'] = (int) $args['range'] - 1; |
| 23 | + if ( !$args['custom_query'] ) |
| 24 | + $args['custom_query'] = @$GLOBALS['wp_query']; |
| 25 | + $count = (int) $args['custom_query']->max_num_pages; |
| 26 | + $page = intval( get_query_var( 'paged' ) ); |
| 27 | + $ceil = ceil( $args['range'] / 2 ); |
| 28 | + |
| 29 | + if ( $count <= 1 ) |
| 30 | + return FALSE; |
| 31 | + |
| 32 | + if ( !$page ) |
| 33 | + $page = 1; |
| 34 | + |
| 35 | + if ( $count > $args['range'] ) { |
| 36 | + if ( $page <= $args['range'] ) { |
| 37 | + $min = 1; |
| 38 | + $max = $args['range'] + 1; |
| 39 | + } elseif ( $page >= ($count - $ceil) ) { |
| 40 | + $min = $count - $args['range']; |
| 41 | + $max = $count; |
| 42 | + } elseif ( $page >= $args['range'] && $page < ($count - $ceil) ) { |
| 43 | + $min = $page - $ceil; |
| 44 | + $max = $page + $ceil; |
| 45 | + } |
| 46 | + } else { |
| 47 | + $min = 1; |
| 48 | + $max = $count; |
| 49 | + } |
| 50 | + |
| 51 | + $echo = ''; |
| 52 | + $previous = intval($page) - 1; |
| 53 | + $previous = esc_attr( get_pagenum_link($previous) ); |
| 54 | + |
| 55 | + $firstpage = esc_attr( get_pagenum_link(1) ); |
| 56 | + if ( $firstpage && (1 != $page) ) |
| 57 | + $echo .= '<li class="previous"><a href="' . $firstpage . '">' . __( 'First', 'text-domain' ) . '</a></li>'; |
| 58 | + |
| 59 | + if ( $previous && (1 != $page) ) |
| 60 | + $echo .= '<li><a href="' . $previous . '" title="' . __( 'previous', 'text-domain') . '">' . $args['previous_string'] . '</a></li>'; |
| 61 | + |
| 62 | + if ( !empty($min) && !empty($max) ) { |
| 63 | + for( $i = $min; $i <= $max; $i++ ) { |
| 64 | + if ($page == $i) { |
| 65 | + $echo .= '<li class="active"><span class="active">' . str_pad( (int)$i, 2, '0', STR_PAD_LEFT ) . '</span></li>'; |
| 66 | + } else { |
| 67 | + $echo .= sprintf( '<li><a href="%s">%002d</a></li>', esc_attr( get_pagenum_link($i) ), $i ); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + $next = intval($page) + 1; |
| 73 | + $next = esc_attr( get_pagenum_link($next) ); |
| 74 | + if ($next && ($count != $page) ) |
| 75 | + $echo .= '<li><a href="' . $next . '" title="' . __( 'next', 'text-domain') . '">' . $args['next_string'] . '</a></li>'; |
| 76 | + |
| 77 | + $lastpage = esc_attr( get_pagenum_link($count) ); |
| 78 | + if ( $lastpage ) { |
| 79 | + $echo .= '<li class="next"><a href="' . $lastpage . '">' . __( 'Last', 'text-domain' ) . '</a></li>'; |
| 80 | + } |
| 81 | + |
| 82 | + if ( isset($echo) ) |
| 83 | + echo $args['before_output'] . $echo . $args['after_output']; |
| 84 | +} |
0 commit comments