Adding Pagination to Your WordPress Theme is Easy as 1-2-3!


SUBMITTED BY: bitcoinmafia

DATE: July 11, 2016, 7:26 p.m.

UPDATED: July 11, 2016, 7:27 p.m.

FORMAT: Text only

SIZE: 3.2 kB

HITS: 1340

  1. What's that, you say? You're building a custom WordPress theme and you need to know how to add pagination? First off, good on ya for going the custom route. You'll end up with a much faster loading and less bloated website that way. Second, you're in luck. I'm going to show you how to add pagination to your WordPress website in just three easy steps. That's it. Just three. Three shall be the counting of the steps and the counting of the steps shall be three. Four shalt thou not count, neither shalt thou count two, excepting that thou then proceedeth to three. Five is right out.
  2. Wait, what? Sorry. Monty Python moment there. Where was I? Oh yes. Three steps.
  3. Step 1
  4. Open up your theme's functions.php and add the following code:
  5. /* Enabling Pagination */
  6. function wp_pagination() {
  7. global $wp_query;
  8. $big = 12345678;
  9. $page_format = paginate_links( array(
  10. 'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
  11. 'format' => '?paged=%#%',
  12. 'current' => max( 1, get_query_var('paged') ),
  13. 'total' => $wp_query->max_num_pages,
  14. 'type' => 'array'
  15. ) );
  16. if( is_array($page_format) ) {
  17. $paged = ( get_query_var('paged') == 0 ) ? 1 : get_query_var('paged');
  18. echo '<div class="pagination"><ul>';
  19. echo '<li><span>Page '. $paged . ' of ' . $wp_query->max_num_pages .'</span></li>';
  20. foreach ( $page_format as $page ) {
  21. echo "<li>$page</li>";
  22. }
  23. echo '</ul></div>';
  24. }
  25. }
  26. As long as you're not putting it smack dab in the middle of another function, it doesn't matter where in your functions.php file you place it. Just for sh*ts and giggles though, put it at the end of the file, right before the closing ?> tag.
  27. Step 2
  28. Open up your category.php file and add the following code AFTER your post loop:
  29. <?php wp_pagination(); ?>
  30. This calls the pagination function you added in Step 1 and renders it on your category pages.
  31. IMPORTANT: Be sure that you add the code AFTER the <?php endif ?> in your post loop.
  32. Step 3
  33. At this point, you'll probably see something like this at the bottom of your category pages:
  34. 1
  35. 2
  36. 3
  37. 4
  38. Not very attractive, is it? So the final step is to style your pagination list items to make them look nicer and match the look and feel of the rest of your WordPress theme.
  39. This is the CSS I used to style the pagination in the header image of this post:
  40. /* Pagination */
  41. .pagination {
  42. margin: 20px 0;
  43. }
  44. .pagination ul {
  45. display: inline-block;
  46. margin-left: 0;
  47. margin-bottom: 0;
  48. border-radius: 5px;
  49. }
  50. .pagination ul>li {
  51. display: inline;
  52. }
  53. .pagination ul>li:first-child>a, .pagination ul>li:first-child>span {
  54. border-left-width: 1px;
  55. border-radius: 5px 0 0 5px;
  56. }
  57. .pagination ul>li:last-child>a, .pagination ul>li:last-child>span {
  58. border-rigth-width: 1px;
  59. border-radius: 0 5px 5px 0;
  60. }
  61. .pagination ul>li>a, .pagination ul>li>span {
  62. float: left;
  63. padding: 4px 12px;
  64. line-height: 20px;
  65. text-decoration: none;
  66. background-color: #ffffff;
  67. border: 1px solid #dddddd;
  68. border-left-width: 0;
  69. }
  70. Conclusion
  71. So there you have it. Give it a try and comment below with your results. I'd love to see what you come up with. If you run into any problems, let me know.

comments powered by Disqus