找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
积分等级发帖收益的说明江阴论坛帮助汇总江阴论坛管理规则(必看)江阴论坛版主招聘中江阴论坛已运行
查看: 2566|回复: 0

[WordPress] wordpress非插件版页面分页

[复制链接]
发表于 2009-12-8 11:06:53 | 显示全部楼层 |阅读模式
使用worpdress的朋友都知道,它的分页采用的是“上一页、下一页”的方式,对于文章太多的朋友不是太方面,当然你可以使用一些翻页的插件来实现如 “pages:1 of 10 1 2 3 4 …”的效果,本人一直都不太喜欢使用插件,正好手头有个不错的方法来实现。

1. 打开你主题模板的functions.php,在里面添加以下代码:
  1. <?php function wp_pagenavi($before = '', $after = '', $prelabel = '', $nxtlabel = '', $pages_to_show = 5, $always_show = false) {
  2. global $request, $posts_per_page, $wpdb, $paged;
  3. if(empty($prelabel)) { $prelabel = '<strong>上一页</strong>';
  4. } if(empty($nxtlabel)) {
  5. $nxtlabel = '<strong>下一页</strong>';
  6. } $half_pages_to_show = round($pages_to_show/2);
  7. if (!is_single()) {
  8. if(!is_category()) {
  9. preg_match('#FROM\s(.*)\sORDER BY#siU', $request, $matches); } else {
  10. preg_match('#FROM\s(.*)\sGROUP BY#siU', $request, $matches); }
  11. $fromwhere = $matches[1];
  12. $numposts = $wpdb->get_var("SELECT COUNT(DISTINCT ID) FROM $fromwhere");
  13. $max_page = ceil($numposts /$posts_per_page);
  14. if(empty($paged)) {
  15. $paged = 1;
  16. }
  17. if($max_page > 1 || $always_show) {
  18. echo "$before <div class='pagers'><span>当前第 $paged / $max_page 页 : </span>"; if ($paged >= ($pages_to_show-1)) {
  19. echo '<a href="'.get_pagenum_link().'">&laquo; 首页</a>'; }
  20. previous_posts_link($prelabel);
  21. for($i = $paged - $half_pages_to_show; $i <= $paged + $half_pages_to_show; $i++) { if ($i >= 1 && $i <= $max_page) { if($i == $paged) {
  22. echo "<strong>$i</strong>";
  23. } else {
  24. echo '<a href="'.get_pagenum_link($i).'">'.$i.'</a>'; }
  25. }
  26. }
  27. next_posts_link($nxtlabel, $max_page);
  28. if (($paged+$half_pages_to_show) < ($max_page)) {
  29. echo '<a href="'.get_pagenum_link($max_page).'">最后 &raquo;</a>'; }
  30. echo "</div> $after";
  31. }
  32. }
  33. }
  34. ?>
复制代码
2. 在index.php archive.php等需要分页的地方使用
  1. <?php wp_pagenavi() ?>
复制代码
替换掉
  1. <?php previous_post_link('上一篇: %link') ?>
  2. <?php next_post_link('%link :下一篇') ?>
复制代码
本站的样式.
  1. .pagers{height:25px;color:#111;padding:2px 0;margin:0;text-align:center;}
  2. .pagers a,.pagers strong{margin:0 2px;padding:2px 5px;}
  3. .pagers a{border:1px solid #6c8c37}
  4. .pagers strong{border:1px solid #111}
复制代码
原文:http://www.heibaiy.com/wordpress-non-plugin-page-navi.html
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|江阴人家

GMT+8, 2024-4-19 23:04 , Processed in 0.017467 second(s), 16 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表