firoze

post pagination without plugin

Jul 19th, 2015
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. // How to Add Numeric Pagination in Your WordPress Theme without plugin
  2.  
  3. <!-- call this into functions.php -->
  4. function wpbeginner_numeric_posts_nav() {
  5.  
  6. if( is_singular() )
  7. return;
  8.  
  9. global $wp_query;
  10.  
  11. /** Stop execution if there's only 1 page */
  12. if( $wp_query->max_num_pages <= 1 )
  13. return;
  14.  
  15. $paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
  16. $max = intval( $wp_query->max_num_pages );
  17.  
  18. /** Add current page to the array */
  19. if ( $paged >= 1 )
  20. $links[] = $paged;
  21.  
  22. /** Add the pages around the current page to the array */
  23. if ( $paged >= 3 ) {
  24. $links[] = $paged - 1;
  25. $links[] = $paged - 2;
  26. }
  27.  
  28. if ( ( $paged + 2 ) <= $max ) {
  29. $links[] = $paged + 2;
  30. $links[] = $paged + 1;
  31. }
  32.  
  33. echo '<div class="navigation"><ul>' . "\n";
  34.  
  35. /** Previous Post Link */
  36. if ( get_previous_posts_link() )
  37. printf( '<li>%s</li>' . "\n", get_previous_posts_link() );
  38.  
  39. /** Link to first page, plus ellipses if necessary */
  40. if ( ! in_array( 1, $links ) ) {
  41. $class = 1 == $paged ? ' class="active"' : '';
  42.  
  43. printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );
  44.  
  45. if ( ! in_array( 2, $links ) )
  46. echo '<li>…</li>';
  47. }
  48.  
  49. /** Link to current page, plus 2 pages in either direction if necessary */
  50. sort( $links );
  51. foreach ( (array) $links as $link ) {
  52. $class = $paged == $link ? ' class="active"' : '';
  53. printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link );
  54. }
  55.  
  56. /** Link to last page, plus ellipses if necessary */
  57. if ( ! in_array( $max, $links ) ) {
  58. if ( ! in_array( $max - 1, $links ) )
  59. echo '<li>…</li>' . "\n";
  60.  
  61. $class = $paged == $max ? ' class="active"' : '';
  62. printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
  63. }
  64.  
  65. /** Next Post Link */
  66. if ( get_next_posts_link() )
  67. printf( '<li>%s</li>' . "\n", get_next_posts_link() );
  68.  
  69. echo '</ul></div>' . "\n";
  70.  
  71. }
  72.  
  73. *****************************************************************************************************************
  74.  
  75. <!-- call this just the post loop where <?php endwhile;?>-->
  76. <?php wpbeginner_numeric_posts_nav(); ?>
  77.  
  78.  
  79. ******************************************************************************************************************
  80.  
  81. <!-- this is the pagition css -->
  82.  
  83. .navigation li a,
  84. .navigation li a:hover,
  85. .navigation li.active a,
  86. .navigation li.disabled {
  87. color: #fff;
  88. text-decoration:none;
  89. }
  90.  
  91. .navigation li {
  92. display: inline;
  93. }
  94.  
  95. .navigation li a,
  96. .navigation li a:hover,
  97. .navigation li.active a,
  98. .navigation li.disabled {
  99. background-color: #6FB7E9;
  100. border-radius: 3px;
  101. cursor: pointer;
  102. padding: 12px;
  103. padding: 0.75rem;
  104. }
  105.  
  106. .navigation li a:hover,
  107. .navigation li.active a {
  108. background-color: #3C8DC5;
  109. }
  110. div.navigation ul li a{line-height:50px;}
  111. *****************************************************************************************
  112.  
  113. source link: http://www.wpbeginner.com/wp-themes/how-to-add-numeric-pagination-in-your-wordpress-theme/
Advertisement
Add Comment
Please, Sign In to add comment