PageRenderTime 36ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/pagination.php

https://github.com/alejandropereira/adomicilio
PHP | 70 lines | 43 code | 19 blank | 8 comment | 14 complexity | ceaea2c2c479aadbd81d493615f4cdae MD5 | raw file
  1. <?php
  2. function get_pagination() {
  3. if( is_singular() )
  4. return;
  5. global $wp_query;
  6. /** Stop execution if there's only 1 page */
  7. if( $wp_query->max_num_pages <= 1 )
  8. return;
  9. $paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
  10. $max = intval( $wp_query->max_num_pages );
  11. /** Add current page to the array */
  12. if ( $paged >= 1 )
  13. $links[] = $paged;
  14. /** Add the pages around the current page to the array */
  15. if ( $paged >= 3 ) {
  16. $links[] = $paged - 1;
  17. $links[] = $paged - 2;
  18. }
  19. if ( ( $paged + 2 ) <= $max ) {
  20. $links[] = $paged + 2;
  21. $links[] = $paged + 1;
  22. }
  23. echo '<div class="navigation"><ul>' . "\n";
  24. /** Previous Post Link */
  25. if ( get_previous_posts_link() )
  26. printf( '<li class="prev">%s</li>' . "\n", get_previous_posts_link("Anterior") );
  27. /** Link to first page, plus ellipses if necessary */
  28. if ( ! in_array( 1, $links ) ) {
  29. $class = 1 == $paged ? ' class="active"' : '';
  30. printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );
  31. if ( ! in_array( 2, $links ) )
  32. echo '<li>…</li>';
  33. }
  34. /** Link to current page, plus 2 pages in either direction if necessary */
  35. sort( $links );
  36. foreach ( (array) $links as $link ) {
  37. $class = $paged == $link ? ' class="active"' : '';
  38. printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link );
  39. }
  40. /** Link to last page, plus ellipses if necessary */
  41. if ( ! in_array( $max, $links ) ) {
  42. if ( ! in_array( $max - 1, $links ) )
  43. echo '<li>…</li>' . "\n";
  44. $class = $paged == $max ? ' class="active"' : '';
  45. printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
  46. }
  47. /** Next Post Link */
  48. if ( get_next_posts_link() )
  49. printf( '<li class="next">%s</li>' . "\n", get_next_posts_link("Sig.") );
  50. echo '</ul></div>' . "\n";
  51. }