Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. function get_pagination($range = 4){
  2. // $paged - number of the current page
  3. global $paged, $wp_query;
  4. // How much pages do we have?
  5. if ( !$max_page ) {
  6. $max_page = $wp_query->max_num_pages;
  7. }
  8. // We need the pagination only if there are more than 1 page
  9. if($max_page > 1){
  10. if(!$paged){
  11. $paged = 1;
  12. }
  13. // On the first page, don't put the First page link
  14. if($paged != 1){
  15. echo "<a href=" . get_pagenum_link(1) . "> First </a>";
  16. }
  17. // To the previous page
  18. previous_posts_link(' « ');
  19. // We need the sliding effect only if there are more pages than is the sliding range
  20. if($max_page > $range){
  21. // When closer to the beginning
  22. if($paged < $range){
  23. for($i = 1; $i <= ($range + 1); $i++){
  24. echo "<a href='" . get_pagenum_link($i) ."'";
  25. if($i==$paged) echo "class='current'";
  26. echo ">$i</a>";
  27. }
  28. }
  29. // When closer to the end
  30. elseif($paged >= ($max_page - ceil(($range/2)))){
  31. for($i = $max_page - $range; $i <= $max_page; $i++){
  32. echo "<a href='" . get_pagenum_link($i) ."'";
  33. if($i==$paged) echo "class='current'";
  34. echo ">$i</a>";
  35. }
  36. }
  37. // Somewhere in the middle
  38. elseif($paged >= $range && $paged < ($max_page - ceil(($range/2)))){
  39. for($i = ($paged - ceil($range/2)); $i <= ($paged + ceil(($range/2))); $i++){
  40. echo "<a href='" . get_pagenum_link($i) ."'";
  41. if($i==$paged) echo "class='current'";
  42. echo ">$i</a>";
  43. }
  44. }
  45. }
  46. // Less pages than the range, no sliding effect needed
  47. else{
  48. for($i = 1; $i <= $max_page; $i++){
  49. echo "<a href='" . get_pagenum_link($i) ."'";
  50. if($i==$paged) echo "class='current'";
  51. echo ">$i</a>";
  52. }
  53. }
  54. // Next page
  55. next_posts_link(' » ');
  56. // On the last page, don't put the Last page link
  57. if($paged != $max_page){
  58. echo " <a href=" . get_pagenum_link($max_page) . "> Last </a>";
  59. }
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement