Wordpress custom loop with pagination


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



Copy this code and paste it in your HTML
  1. <?php
  2. $blog = new WP_Query();
  3. $args = array(
  4. 'posts_per_page' => 5,
  5. 'paged' => $paged
  6. );
  7. $blog->query($args);
  8. if($blog->have_posts()):
  9. while ($blog->have_posts()):
  10. $blog->the_post(); // Sets up the post
  11. ?>
  12. <h2><a href="<?php the_permalink() ?>"><?php the_title() ?></a></h2>
  13. <?php the_excerpt() ?>
  14. <?php
  15. endwhile;
  16. /* -----------------------
  17.   FUNCTION IS BELOW
  18. ----------------------- */
  19. wp_pagination_nav($blog, $paged);
  20. wp_reset_query();
  21. endif;
  22. ?>
  23. <?php
  24. /* --------------------------------------
  25.   ADD PAGINATION FUNCTION TO YOUR
  26.   FUNCTIONS.PHP FILE
  27. -------------------------------------- */
  28. function wp_pagination_nav($loop, $paged){
  29. if($loop->max_num_pages>1):
  30. echo '<div class="pagination">';
  31. if ($paged > 1):
  32. echo '<a href="?paged='.($paged-1).'"><</a> ';
  33. endif;
  34.  
  35. for($i=1;$i<=$loop->max_num_pages;$i++){
  36. if($paged==$i || ($i==1 && $paged == 0)){
  37. $class = "selected";
  38. } else {
  39. $class = '';
  40. }
  41. echo '<a href="?paged='.$i.'" class="'.$class.'">'.$i.'</a> ';
  42. }
  43.  
  44. if($paged < $loop->max_num_pages):
  45. $nextpage = $paged+1;
  46. if($paged == 0) $nextpage = 2;
  47. echo '<a href="?paged='.$nextpage.'">></a> ';
  48. endif;
  49. echo '<div class="clear"></div></div>';
  50. endif;
  51. }
  52. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.