wordpress分页功能实现

11-15 | 夜光 | IT记录

一、在主题functions.php中添加如下代码:

/*pagination_start*/
function ylife_pagination($query_string){
	global $posts_per_page, $paged;
	$my_query = new WP_Query($query_string ."&posts_per_page=-1");
	$total_posts = $my_query->post_count;
	if(empty($paged))$paged = 1;
		$prev = $paged - 1;
		$next = $paged + 1;
		$range = 2; // only edit this if you want to show more page-links
		$showitems = ($range * 2)+1;
		$pages = ceil($total_posts/$posts_per_page);
	if(1 != $pages){
		echo "<div class='pagination'>";
		echo ($paged > 2 && $paged+$range+1 > $pages && $showitems < $pages)? "<a href='".get_pagenum_link(1)."'>".__('First','YLife')."</a>":"";
		echo ($paged > 1 && $showitems < $pages)? "<a href='".get_pagenum_link($prev)."'>".__('Previous','YLife')."</a>":"";
	for ($i=1; $i <= $pages; $i++){
		if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )){
			echo ($paged == $i)? "<span class='current'>".$i."</span>":"<a href='".get_pagenum_link($i)."' class='inactive' >".$i."</a>";
		}
	}
	echo ($paged < $pages && $showitems < $pages) ? "<a href='".get_pagenum_link($next)."'>".__('Next','YLife')."</a>" :"";
	echo ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) ? "<a href='".get_pagenum_link($pages)."'>".__('Last','YLife')."</a>":"";
	echo "</div>\n";
	}
}
/*pagination_end*/

二、在需要分页的页面文件,比如index.php,archive.php中用如下代码调用:

<?php ylife_pagination($query_string); ?>

三、在style.css文件中加入以下代码:

/*pagination_start*/
.pagination {
	line-height:27px;
}
.pagination span, .pagination a {
	font-size:13px;
	margin: 2px 6px 2px 0;
	border:1px solid #e5e5e5;
	padding:2px 6px 2px 6px;
	text-decoration:none;
}
.pagination a:hover {
	background:#666;
	border:1px solid #666;
	color:#fff;
}
.pagination .current {
	border:1px solid #8d8d8d;
	color:#393939;
	font-size:13px;
	padding:2px 6px 2px 6px;
}
/*pagination_end*/
本文标签:
本文链接: wordpress-pagination/
版权所有: 玻璃泉, 转载请注明本文出处。

2个评论

  1. 感谢博主。终于度娘到一篇能用的代码了TAT

  2. 感谢博主啊,哈哈