当前位置:Gxlcms > PHP教程 > WordPress4分页

WordPress4分页

时间:2021-07-01 10:21:17 帮助过:17人阅读

入门菜鸟,希望能给和菜鸟相互取暖

最近在改造wordpress,自己写代码做了个分页
1,在自己主题下的style.css中增加如下css

Js代码

  1. .pagination {
  2. width: auto;
  3. display: block;
  4. text-align: center;
  5. margin: 30px;
  6. }
  7. .pagination a {
  8. background-color: #eee;
  9. text-decoration: none;
  10. color: #999;
  11. font-size: 18px;
  12. padding: 0px 10px;
  13. line-height: 32px;
  14. height: 32px;
  15. margin: 0px 0.5px;
  16. }
  17. .pagination a.page-btn {
  18. width: 40px;
  19. }
  20. .pagination a.page-btn .icon-prev {
  21. position: absolute;
  22. width: 0px;
  23. height: 0px;
  24. border-top: 6px solid transparent;
  25. border-bottom: 6px solid transparent;
  26. border-left: 6px solid transparent;
  27. border-right: 6px solid transparent;
  28. border-left: none;
  29. border-right: 6px solid #999;
  30. left: 50%;
  31. margin-left: -3px;
  32. top: 50%;
  33. margin-top: -6px;
  34. -webkit-transition: all 0.1s ease;
  35. transition: all 0.1s ease;
  36. }
  37. .pagination a.page-btn .icon-next {
  38. position: absolute;
  39. width: 0px;
  40. height: 0px;
  41. border-top: 6px solid transparent;
  42. border-bottom: 6px solid transparent;
  43. border-left: 6px solid transparent;
  44. border-right: 6px solid transparent;
  45. border-right: none;
  46. border-left: 6px solid #999;
  47. left: 50%;
  48. margin-left: -3px;
  49. top: 50%;
  50. margin-top: -6px;
  51. -webkit-transition: all 0.1s ease;
  52. transition: all 0.1s ease;
  53. }
  54. .pagination a:hover {
  55. color: #fff;
  56. }
  57. .pagination a:hover .icon-prev {
  58. width: 0px;
  59. height: 0px;
  60. border-top: 6px solid transparent;
  61. border-bottom: 6px solid transparent;
  62. border-left: 6px solid transparent;
  63. border-right: 6px solid transparent;
  64. border-left: none;
  65. border-right: 6px solid #fff;
  66. }
  67. .pagination a:hover .icon-next {
  68. width: 0px;
  69. height: 0px;
  70. border-top: 6px solid transparent;
  71. border-bottom: 6px solid transparent;
  72. border-left: 6px solid transparent;
  73. border-right: 6px solid transparent;
  74. border-right: none;
  75. border-left: 6px solid #fff;
  76. }
  77. .pagination span {
  78. display: inline-block;
  79. margin: 0px 1px;
  80. color: #adadad;
  81. font-size: 18px;
  82. }

2, 将如下代码copy到自己theme的functions.php中,

Php代码

  1. /**
  2. /**
  3. * @param int $total_pages
  4. * 总页数
  5. * @param int $paged
  6. * 当前页数从1计
  7. * @param int $visible_count
  8. * 可见的页码数,接受自定义,如果自定义小于1或者大于max,置为max
  9. *
  10. *@author shandaiwang
  11. */
  12. define('MAX_VISIBLE_PAGE_COUNT', 20);
  13. function pagination($total_pages = 0, $current_page = 1, $visible_count = MAX_VISIBLE_PAGE_COUNT){
  14. // 入参适配
  15. if($total_pages <= 1) {
  16. return;
  17. }
  18. if($current_page <= 0) {
  19. $current_page = 1;
  20. }
  21. if($visible_count <= 0 || $visible_count > MAX_VISIBLE_PAGE_COUNT) {
  22. $visible_count = MAX_VISIBLE_PAGE_COUNT;
  23. }
  24. // 入参适配结束
  25. // 页码列表适配
  26. $range = ceil(($visible_count + 1) / 2);
  27. $start = $current_page - $range + 1;
  28. if($start < 1) {
  29. $start = 1;
  30. }
  31. $end = $start + $visible_count - 1;
  32. if ($end > $total_pages) {
  33. $end = $total_pages;
  34. // 向前反推,尽可能使显示个数为$visible_count
  35. $start = $end - $visible_count + 1;
  36. if($start < 1) {
  37. $start = 1;
  38. }
  39. }
  40. // 页码列表适配结束
  41. // 渲染
  42. echo "";
  43. if($current_page > 1) { // pre
  44. echo(' ');
  45. }
  46. if($start != 1) { // first[...]
  47. echo('1');
  48. if ($start != 2) {
  49. echo('<span>...</span>');
  50. }
  51. }
  52. for ($i=$start; $i <= $end; $i++){ // list
  53. if ($i == $current_page) {
  54. echo(''.$i.'');
  55. } else {
  56. echo(''.$i.'');
  57. }
  58. }
  59. if($end < $total_pages) { //[...]last
  60. if ($end + 1 < $total_pages) {
  61. echo('<span>...</span>');
  62. }
  63. echo(''.$total_pages.'');
  64. }
  65. if($current_page < $total_pages) { // next
  66. echo(' ');
  67. }
  68. echo "\n";
  69. }

3,在需要的地方引用即可,形如


6f981d79-c23e-3bdc-8503-da23907125a0.png

至于如何获得$total_pages, $current_page,我用的WP query,形如

b6a2a001-186e-338f-837f-bf7fa1a1a6f4.png

人气教程排行