Pagination function


/ Published in: PHP
Save to your folder(s)

Pagination function


Copy this code and paste it in your HTML
  1. // pagination function
  2. function pagination($pages = '', $range = 4)
  3. {
  4. $showitems = ($range * 2)+1;
  5.  
  6. global $paged;
  7. if(empty($paged)) $paged = 1;
  8.  
  9. if($pages == '')
  10. {
  11. global $wp_query;
  12. $pages = $wp_query->max_num_pages;
  13. if(!$pages)
  14. {
  15. $pages = 1;
  16. }
  17. }
  18.  
  19. if(1 != $pages)
  20. {
  21. echo "<div class=\"pagination\"><span>Page ".$paged." of ".$pages."</span>";
  22. if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>&laquo; First</a>";
  23. if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo; Previous</a>";
  24.  
  25. for ($i=1; $i <= $pages; $i++)
  26. {
  27. if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
  28. {
  29. echo ($paged == $i)? "<span class=\"current\">".$i."</span>":"<a href='".get_pagenum_link($i)."' class=\"inactive\">".$i."</a>";
  30. }
  31. }
  32.  
  33. if ($paged < $pages && $showitems < $pages) echo "<a href=\"".get_pagenum_link($paged + 1)."\">Next &rsaquo;</a>";
  34. if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>Last &raquo;</a>";
  35. echo "</div>\n";
  36. }
  37. }
  38.  
  39.  
  40. //call pagination
  41. <?php if (function_exists("pagination")) {
  42. pagination($additional_loop->max_num_pages);
  43. } ?>
  44.  
  45.  
  46. //style pagination
  47. .pagination {
  48. clear:both;
  49. padding:20px 0;
  50. position:relative;
  51. font-size:11px;
  52. line-height:13px;
  53. }
  54.  
  55. .pagination span, .pagination a {
  56. display:block;
  57. float:left;
  58. margin: 2px 2px 2px 0;
  59. padding:6px 9px 5px 9px;
  60. text-decoration:none;
  61. width:auto;
  62. color:#fff;
  63. background: #555;
  64. }
  65.  
  66. .pagination a:hover{
  67. color:#fff;
  68. background: #3279BB;
  69. }
  70.  
  71. .pagination .current{
  72. padding:6px 9px 5px 9px;
  73. background: #3279BB;
  74. color:#fff;
  75. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.