换了个分页导航代码

今天看到zww的Mini Pagenavi 修改版外加“上一页”、“下一页”链接的分页代码,感觉挺不错的,果断用了上去,然后把原来用的分页代码记录下来,方便以后还能用到。

1. 把下面的代码都扔到主题的 functions.php 里面:

//分页导航
function wp_pagenavi($range = 6){
// $paged - number of the current page
global $paged, $wp_query;
// How much pages do we have?
if ( !$max_page ) {
$max_page = $wp_query->max_num_pages;
}
// We need the pagination only if there are more than 1 page
if($max_page > 1){
if(!$paged){
$paged = 1;
}
echo '';
// On the first page, don't put the First page link
echo "<li><a href='" . get_pagenum_link(1) . "' class='extend' title='最前一页'>&laquo;</a></li>";
// To the previous page
echo "<li>";
previous_posts_link('&lsaquo;');
echo "</li>";
// We need the sliding effect only if there are more pages than is the sliding range
if($max_page > $range){
// When closer to the beginning
if($paged < $range){
for($i = 1; $i <= ($range + 1); $i++){
if($i==$paged) echo "<li class='active'><a>$i</a></li>";
else echo "<li><a href='" . get_pagenum_link($i) ."'>$i</a></li>";
}
}
// When closer to the end
elseif($paged >= ($max_page - ceil(($range/2)))){
for($i = $max_page - $range; $i <= $max_page; $i++){
if($i==$paged) echo "<li class='active'><a>$i</a></li>";
else echo "<li><a href='" . get_pagenum_link($i) ."'>$i</a></li>";
}
}
// Somewhere in the middle
elseif($paged >= $range && $paged < ($max_page - ceil(($range/2)))){
for($i = ($paged - ceil($range/2)); $i <= ($paged + ceil(($range/2))); $i++){
if($i==$paged) echo "<li class='active'><a>$i</a></li>";
else echo "<li><a href='" . get_pagenum_link($i) ."'>$i</a></li>";
}
}
}
// Less pages than the range, no sliding effect needed
else{
for($i = 1; $i <= $max_page; $i++){
if($i==$paged) echo "<li class='active'><a>$i</a></li>";
else echo "<li><a href='" . get_pagenum_link($i) ."'>$i</a></li>";
}
}
// Next page
echo "<li>";
next_posts_link('&rsaquo;');
echo "</li>";
// On the last page, don't put the Last page link
echo "<li><a href='" . get_pagenum_link($max_page) . "' class='extend' title='最后一页'>&raquo;</a></li>";
}
}

2. 在 index.php/home.php/archive.php/search.php等页面调用:

<?php wp_pagenavi(6); ?>

曾经的分页导航样式如图:20150123114410

zww的代码如下:

1. 把下面的代码都扔到主题的 functions.php 里面

/* Mini Pagenavi v1.0 by Willin Kan. Edit by zwwooooo */
if ( !function_exists('pagenavi') ) {
	function pagenavi( $p = 5 ) { // 取当前页前后各 2 页
		if ( is_singular() ) return; // 文章与插页不用
		global $wp_query, $paged;
		$max_page = $wp_query->max_num_pages;
		if ( $max_page == 1 ) return; // 只有一页不用
		if ( empty( $paged ) ) $paged = 1;
		echo '<span class="pages">Page: ' . $paged . ' of ' . $max_page . ' </span> '; // 显示页数
		if ( $paged > 1 ) p_link( $paged - 1, '上一页', '« Previous' );/* 如果当前页大于1就显示上一页链接 */
		if ( $paged > $p + 1 ) p_link( 1, '最前页' );
		if ( $paged > $p + 2 ) echo '... ';
		for( $i = $paged - $p; $i <= $paged + $p; $i++ ) { // 中间页
			if ( $i > 0 && $i <= $max_page ) $i == $paged ? print "<span class='page-numbers current'>{$i}</span> " : p_link( $i );
		}
		if ( $paged < $max_page - $p - 1 ) echo '... ';
		if ( $paged < $max_page - $p ) p_link( $max_page, '最后页' );
		if ( $paged < $max_page ) p_link( $paged + 1,'下一页', 'Next »' );/* 如果当前页不是最后一页显示下一页链接 */
	}
	function p_link( $i, $title = '', $linktype = '' ) {
		if ( $title == '' ) $title = "第 {$i} 页";
		if ( $linktype == '' ) { $linktext = $i; } else { $linktext = $linktype; }
		echo "<a class='page-numbers' href='", esc_html( get_pagenum_link( $i ) ), "' title='{$title}'>{$linktext}</a> ";
	}
}
// -- END ----------------------------------------

2. 在 index.php/home.php/archive.php/search.php等页面调用

<?php if (function_exists('pagenavi')) { pagenavi(); } ?>

 

本文共 6 个回复

  1. 王语双 says:
    :cool: 好折腾,可以搞个培训班了,人家说可以月入过万。 :idea:
  2. Youth.霖 says:
    支持代码君~~ :!: 在默认的主题里有分页的代码也可以参考

发表留言,喵!