Guest User

Untitled

a guest
Jul 10th, 2020
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. /**
  2. * Displays a page pagination if more posts are available than can be displayed on one page
  3. *
  4. * @param string|WP_Query $pages pass the number of pages instead of letting the script check the gobal paged var
  5. * @param string $wrapper
  6. * @return string returns the pagination html code
  7. */
  8. function avia_pagination($pages = '', $wrapper = 'div') //pages is either the already calculated number of pages or the wp_query object
  9. {
  10. global $paged, $wp_query;
  11.  
  12. if(is_object($pages))
  13. {
  14. $use_query = $pages;
  15. $pages = '';
  16. }
  17. else
  18. {
  19. $use_query = $wp_query;
  20. }
  21.  
  22. if(get_query_var('paged')) {
  23. $paged = get_query_var('paged');
  24. } elseif(get_query_var('page')) {
  25. $paged = get_query_var('page');
  26. } else {
  27. $paged = 1;
  28. }
  29.  
  30. $output = '';
  31. $prev = $paged - 1;
  32. $next = $paged + 1;
  33. $range = 2; // only edit this if you want to show more page-links
  34. $showitems = ($range * 2)+1;
  35.  
  36. if(is_page(array( 731, 5494, 2345))) {
  37. $pages = 2;
  38. }
  39.  
  40. if($pages == '') //if the default pages are used
  41. {
  42. //$pages = ceil(wp_count_posts($post_type)->publish / $per_page);
  43. $pages = $use_query->max_num_pages;
  44.  
  45. if(!$pages)
  46. {
  47. $pages = 1;
  48. }
  49.  
  50. //factor in pagination
  51. if( isset($use_query->query) && !empty($use_query->query['offset']) && $pages > 1 )
  52. {
  53. $offset_origin = $use_query->query['offset'] - ($use_query->query['posts_per_page'] * ( $paged - 1 ) );
  54. $real_posts = $use_query->found_posts - $offset_origin;
  55. $pages = ceil( $real_posts / $use_query->query['posts_per_page']);
  56. }
  57. }
  58.  
  59.  
  60. $method = is_single() ? 'avia_post_pagination_link' : 'get_pagenum_link';
  61.  
  62. /**
  63. * Allows to change pagination method
  64. *
  65. * @used_by avia_sc_blog 10
  66. * @since 4.5.6
  67. * @return string
  68. */
  69. $method = apply_filters( 'avf_pagination_link_method', $method, $pages, $wrapper );
  70.  
  71. if(1 != $pages)
  72. {
  73. $output .= "<$wrapper class='pagination'>";
  74. $output .= "<span class='pagination-meta'>".sprintf(__("Page %d of %d", 'avia_framework'), $paged, $pages)."</span>";
  75. $output .= ($paged > 2 && $paged > $range+1 && $showitems < $pages)? "<a href='".$method(1)."'>&laquo;</a>":'';
  76. $output .= ($paged > 1 && $showitems < $pages)? "<a href='".$method($prev)."'>&lsaquo;</a>":'';
  77.  
  78.  
  79. for ($i=1; $i <= $pages; $i++)
  80. {
  81. if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
  82. {
  83. switch( $i )
  84. {
  85. case ( $paged == $i ):
  86. $class = 'current';
  87. break;
  88. case ( ( $paged - 1 ) == $i ):
  89. $class = 'inactive previous_page';
  90. break;
  91. case ( ( $paged + 1 ) == $i ):
  92. $class = 'inactive next_page';
  93. break;
  94. default:
  95. $class = 'inactive';
  96. break;
  97. }
  98.  
  99. $output .= ( $paged == $i )? "<span class='{$class}'>" . $i . "</span>" : "<a href='" . $method($i) . "' class='{$class}' >" . $i . "</a>";
  100. }
  101. }
  102.  
  103. $output .= ($paged < $pages && $showitems < $pages) ? "<a href='".$method($next)."'>&rsaquo;</a>" :'';
  104. $output .= ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) ? "<a href='".$method($pages)."'>&raquo;</a>":'';
  105. $output .= "</$wrapper>\n";
  106. }
  107.  
  108. return apply_filters( 'avf_pagination_output', $output, $paged, $pages, $wrapper );
  109. }
  110.  
  111. /**
  112. *
  113. * @since < 4.5 - modified 4.5.5
  114. * @param int $page_number
  115. * @return string
  116. */
  117. function avia_post_pagination_link( $page_number )
  118. {
  119. global $post;
  120.  
  121. //the _wp_link_page uses get_permalink() which might be changed by a query. we need to get the original post id temporarily
  122. $temp_post = $post;
  123. // $post = get_post(avia_get_the_id());
  124.  
  125. /**
  126. * With WP 5.1 returns an extra class that breaks our HTML link
  127. */
  128. $html = _wp_link_page( $page_number );
  129.  
  130. $match = array();
  131. preg_match('/href=["\']?([^"\'>]+)["\']?/', $html, $match );
  132. $url = isset( $match[1] ) ? $match[1] : '';
  133.  
  134. $post = $temp_post;
  135.  
  136. /**
  137. * @since 4.5.5
  138. * @return string
  139. */
  140. return apply_filters( 'avf_pagination_post_pagination_link', $url, $page_number );
  141. }
Add Comment
Please, Sign In to add comment