Paginate WordPress Posts


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

Place the function in your functions.php file of your theme...

Then, simply call like this: wp_paginate('', 3);
The first param is optional, the second dictates how many pages before and after the current page link will show...


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3.  
  4.  
  5. function wp_paginate($pgs = '', $rng = 2) {
  6. $itm = ($rng * 2) + 1;
  7. global $pgd;
  8. if (empty($pgd))
  9. $pgd = 1;
  10. if ($pgs == '') {
  11. global $wp_query;
  12. $pgs = $wp_query->max_num_pgs;
  13. if (!$pgs) {
  14. $pgs = 1;
  15. }
  16. }
  17. if (1 != $pgs) {
  18. // echo the HTML output
  19. echo "<div class='wp_paginate'>";
  20. if ($pgd > 2 && $pgd > $rng + 1 && $itm < $pgs)
  21. echo "<a href='" . get_pagenum_link(1) . "'>&laquo;</a>";
  22. if ($pgd > 1 && $itm < $pgs)
  23. echo "<a href='" . get_pagenum_link($pgd - 1) . "'>&lsaquo;</a>";
  24. for ($i = 1; $i <= $pgs; $i++) {
  25. if (1 != $pgs && (!($i >= $pgd + $rng + 1 || $i <= $pgd - $rng - 1) || $pgs <= $itm)) {
  26. echo ($pgd == $i) ? "<span class='current'>" . $i . "</span>" : "<a href='" . get_pagenum_link($i) . "' class='inactive' >" . $i . "</a>";
  27. }
  28. }
  29. if ($pgd < $pgs && $itm < $pgs)
  30. echo "<a href='" . get_pagenum_link($pgd + 1) . "'>&rsaquo;</a>";
  31. if ($pgd < $pgs - 1 && $pgd + $rng - 1 < $pgs && $itm < $pgs)
  32. echo "<a href='" . get_pagenum_link($pgs) . "'>&raquo;</a>";
  33. echo "</div>\n";
  34. }
  35. }
  36. ?>

URL: http://www.design-ninja.net

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.