Pagination WordPress without plugin


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

I dont remember the author this code.


Copy this code and paste it in your HTML
  1. function paglink($first=1,$last=1,$middle=10,$baseURL=false,$wp_query=false ) {
  2. if(!$baseURL) $baseURL= get_bloginfo('url');
  3. if(!$wp_query)global $wp_query;
  4. $page = $wp_query->query_vars["paged"];
  5. if ( !$page ) $page = 1;
  6. $qs = $_SERVER["QUERY_STRING"] ? "?".$_SERVER["QUERY_STRING"] : "";
  7. if ( $wp_query->found_posts > $wp_query->query_vars["posts_per_page"] ) {
  8. echo '<div class="box-paginacao">';
  9. if ( $page > 1 ) {
  10. echo '<a href="'.$baseURL.(($page==2)?('page/'.($page-1).'/'):'').$qs.'" class="link-anterior">anterior</a>';
  11. }
  12. else {
  13. echo '<div class="link-anterior-sem">anterior</div>';
  14. }
  15.  
  16.  
  17. $dots=false;
  18.  
  19. echo '<div class="paginas">';
  20. for ( $i=1; $i <= $wp_query->max_num_pages; $i++ ){ // Loop through pages
  21. if($i<=$first || $i<=$middle && $page<$middle || $i>$wp_query->max_num_pages-$last || $i>$wp_query->max_num_pages-$middle && $page>$wp_query->max_num_pages-$middle+1 || $i>$page-ceil($middle/2) && $i<=$page+floor($middle/2)){
  22. if ( $i == $page ) { // Current page or linked page?
  23. echo '<strong>'.$i.'</strong>';
  24. } else {
  25. echo '<a href="'.$baseURL.(($i!=1)?('page/'.$i.'/'):'').$qs.'">'.$i.'</a>';
  26. }
  27. $dots=false;
  28. }elseif(!$dots){
  29. echo '<span>...</span>';
  30. $dots=true;
  31. }
  32. }
  33.  
  34. echo '</div>';
  35.  
  36. if ( $page < $wp_query->max_num_pages ) { // Next link?
  37. echo '<a href="'.$baseURL.'page/'.($page+1).'/'.$qs.'" class="link-proximo">próximo</a>';
  38. }
  39. else {
  40. echo '<div class="link-proximo-sem">próximo</div>';
  41. }
  42. echo '</div>';
  43. }
  44. }
  45.  
  46. //use function
  47. paglink(1, 1, 10, get_bloginfo('wpurl').'/your_post_type_or_categories/', $wp_query);

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.