Skip to content

Commit

Permalink
添加分页函数
Browse files Browse the repository at this point in the history
  • Loading branch information
iwillhappy1314 committed Jan 6, 2018
1 parent 361d1e4 commit de34020
Showing 1 changed file with 62 additions and 3 deletions.
65 changes: 62 additions & 3 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ function wprs_get_ip() {
/**
* 获取前端资源路径
*/
if ( !class_exists( 'JsonManifest' ) ) {
class JsonManifest {
if ( ! class_exists( 'WprsJsonManifest' ) ) {
class WprsJsonManifest {
private $manifest;

public function __construct( $manifest_path ) {
Expand Down Expand Up @@ -167,7 +167,7 @@ function wprs_asset( $filename ) {

if ( empty( $manifest ) ) {
$manifest_path = get_theme_file_path( '/front/dist/' . 'assets.json' );
$manifest = new JsonManifest( $manifest_path );
$manifest = new WprsJsonManifest( $manifest_path );
}

if ( array_key_exists( $file, $manifest->get() ) ) {
Expand All @@ -176,4 +176,63 @@ function wprs_asset( $filename ) {
return $dist_path . $directory . $file;
}
}
}


/**
* 数字分页
*
* @param string $query
* @param string $pages
* @param int $range
*/
function wprs_pagination( $query = '', $pages = '', $range = 5 ) {
$show_items = ( $range * 2 ) + 1;

global $paged;
if ( empty( $paged ) ) {
$paged = 1;
}

if ( ! $query ) {
global $wp_query;
$wizhi_query = $wp_query;
} else {
$wizhi_query = $query;
}

if ( $pages == '' ) {
$pages = $wizhi_query->max_num_pages;
if ( ! $pages ) {
$pages = 1;
}
}

if ( 1 != $pages ) {
echo '<ul class="pagination">';
if ( $paged > 2 && $paged > $range + 1 && $show_items < $pages ) {
echo '<li><a aria-label="Previous" href="' . get_pagenum_link( 1 ) . '"><span aria-hidden="true">«</span></a></li>';
}
if ( $paged > 1 && $show_items < $pages ) {
echo '<li><a aria-label="Previous" href="' . get_pagenum_link( $paged - 1 ) . '"><span aria-hidden="true"><</span></a></li>';
}

for ( $i = 1; $i <= $pages; $i ++ ) {
if ( 1 != $pages && ( ! ( $i >= $paged + $range + 1 || $i <= $paged - $range - 1 ) || $pages <= $show_items ) ) {
if ( $paged == $i ) {
echo '<li class="active"><a href="#">' . $i . '</a></li>';
} else {
echo '<li><a href="' . get_pagenum_link( $i ) . '">' . $i . '</a></li>';
}
}
}

if ( $paged < $pages ) {
echo '<li><a class="nextpostslink" aria-label="Next" href="' . get_pagenum_link( $paged + 1 ) . '"><span aria-hidden="true">></span></a></li>';
}
if ( $paged < $pages - 1 && $paged + $range - 1 < $pages && $show_items < $pages ) {
echo '<li><a class="lastpostslink" aria-label="Next" href="' . get_pagenum_link( $pages ) . '"><span aria-hidden="true">»</span></a></li>';
}
echo '</ul>';
}
}

0 comments on commit de34020

Please sign in to comment.