wordpress分页方法,不使用插件

function 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;
if($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($pages<=1)return;
if(1 != $pages){
echo '<div class="pagination">';
echo ($paged > 1)? "<a href='".get_pagenum_link(1)."'>第一页</a> ":"第一页 ";
echo ($paged > 1 )? "<a href='".get_pagenum_link($prev)."'>上一页</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) ? "<a href='".get_pagenum_link($next)."'>下一页</a> " :"下一页 ";
echo ($paged!=$pages) ? "<a href='".get_pagenum_link($pages)."'>最后一页</a> ":"最后一页 ";
echo ' 共<font color=red><strong>'.$total_posts.'</strong></font>条记录 每页<font color=red><strong>'.$posts_per_page.'</strong></font>条 共<font color=red><strong>'.$pages.'</strong></font>页 当前第<font color=red><strong>'.$paged.'</strong></font>页</div>';
}
}

posted @ 2012-03-29 18:56  HTL  阅读(924)  评论(0编辑  收藏  举报
htl