Advertisement
Guest User

wpsc_pagination

a guest
Jun 17th, 2012
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.98 KB | None | 0 0
  1. function wpsc_pagination($totalpages = '', $per_page = '', $current_page = '', $page_link = '') {
  2. global $wp_query;
  3. $num_paged_links = 4; //amount of links to show on either side of current page
  4.  
  5. $additional_links = '';
  6.  
  7. //additional links, items per page and products order
  8. if( get_option('permalink_structure') != '' ){
  9. $additional_links_separator = '?';
  10. }else{
  11. $additional_links_separator = '&';
  12. }
  13. if( !empty( $_GET['items_per_page'] ) ){
  14. $additional_links = $additional_links_separator . 'items_per_page=' . $_GET['items_per_page'];
  15. $additional_links_separator = '&';
  16. }
  17. if( !empty( $_GET['product_order'] ) )
  18. $additional_links .= $additional_links_separator . 'product_order=' . $_GET['product_order'];
  19.  
  20. $additional_links = apply_filters('wpsc_pagination_additional_links', $additional_links);
  21. //end of additional links
  22.  
  23. if(empty($totalpages)){
  24. $totalpages = $wp_query->max_num_pages;
  25. }
  26. if(empty($per_page))
  27. $per_page = (int)get_option('wpsc_products_per_page');
  28.  
  29. $current_page = absint( get_query_var('paged') );
  30. if($current_page == 0)
  31. $current_page = 1;
  32.  
  33. if(empty($page_link))
  34. $page_link = wpsc_a_page_url();
  35.  
  36. //if there is no pagination
  37. if(!get_option('permalink_structure')) {
  38. $category = '?';
  39. if(isset($wp_query->query_vars['wpsc_product_category']))
  40. $category = '?wpsc_product_category='.$wp_query->query_vars['wpsc_product_category'];
  41. if(isset($wp_query->query_vars['wpsc_product_category']) && is_string($wp_query->query_vars['wpsc_product_category'])){
  42.  
  43. $page_link = get_option('blogurl').$category.'&paged';
  44. }else{
  45. $page_link = get_option('product_list_url').$category.'&paged';
  46. }
  47.  
  48. $separator = '=';
  49. }else{
  50. if ( isset( $wp_query->query_vars['wpsc_product_category'] ) ) {
  51. $category_id = get_term_by( 'slug', $wp_query->query_vars['wpsc_product_category'], 'wpsc_product_category' );
  52. $page_link = trailingslashit( get_term_link( $category_id, 'wpsc_product_category' ) );
  53. } else {
  54. $page_link = trailingslashit( get_option( 'product_list_url' ) );
  55. }
  56. $separator = 'page/';
  57. }
  58.  
  59. // If there's only one page, return now and don't bother
  60. if($totalpages == 1)
  61. return;
  62. // Pagination Prefix
  63. $output = __('Pages: ','wpsc');
  64.  
  65. if(get_option('permalink_structure')){
  66. // Should we show the FIRST PAGE link?
  67. if($current_page > 2)
  68. $output .= "<a href=\"". esc_url( $page_link . $additional_links ) . "\" title=\"" . __('First Page', 'wpsc') . "\">" . __('&laquo; First', 'wpsc') . "</a>";
  69.  
  70. // Should we show the PREVIOUS PAGE link?
  71. if($current_page > 1) {
  72. $previous_page = $current_page - 1;
  73. if( $previous_page == 1 )
  74. $output .= " <a href=\"". esc_url( $page_link . $additional_links ) . "\" title=\"" . __('Previous Page', 'wpsc') . "\">" . __('&lt; Previous', 'wpsc') . "</a> /";
  75. else
  76. $output .= " <a href=\"". esc_url( $page_link .$separator. $previous_page . $additional_links ) . "\" title=\"" . __('Previous Page', 'wpsc') . "\">" . __('&lt; Previous', 'wpsc') . "</a> /";
  77. }
  78. $i =$current_page - $num_paged_links;
  79. $count = 1;
  80. if($i <= 0) $i =1;
  81. while($i < $current_page){
  82. if($count <= $num_paged_links){
  83. if($count == 1)
  84. $output .= "<a href=\"". esc_url( $page_link . $additional_links ) . "\" title=\"" . sprintf( __('Page %s', 'wpsc'), $i ) . " \">".$i."</a>";
  85. else
  86. $output .= " <a href=\"". esc_url( $page_link .$separator. $i . $additional_links ) . "\" title=\"" . sprintf( __('Page %s', 'wpsc'), $i ) . " \">".$i."</a>";
  87. }
  88. $i++;
  89. $count++;
  90. }
  91. // Current Page Number
  92. if($current_page > 0)
  93. $output .= "<span class='current'>$current_page</span>";
  94.  
  95. //Links after Current Page
  96. $i = $current_page + $num_paged_links;
  97. $count = 1;
  98.  
  99. if($current_page < $totalpages){
  100. while(($i) > $current_page){
  101.  
  102. if($count < $num_paged_links && ($count+$current_page) <= $totalpages){
  103. $output .= " <a href=\"". esc_url( $page_link .$separator. ($count+$current_page) .$additional_links ) . "\" title=\"" . sprintf( __('Page %s', 'wpsc'), ($count+$current_page) ) . "\">".($count+$current_page)."</a>";
  104. $i++;
  105. }else{
  106. break;
  107. }
  108. $count ++;
  109. }
  110. }
  111.  
  112. if($current_page < $totalpages) {
  113. $next_page = $current_page + 1;
  114. $output .= "<span>/</span><a href=\"". esc_url( $page_link .$separator. $next_page . $additional_links ) . "\" title=\"" . __('Next Page', 'wpsc') . "\">" . __('Next &gt;', 'wpsc') . "</a>";
  115. }
  116. // Should we show the LAST PAGE link?
  117. if($current_page < $totalpages - 3) {
  118. $output .= "<a href=\"". esc_url( $page_link .$separator. $totalpages . $additional_links ) . "\" title=\"" . __('Last Page', 'wpsc') . "\">" . __('Last &raquo;', 'wpsc') . "</a>";
  119. }
  120. } else {
  121. // Should we show the FIRST PAGE link?
  122. if($current_page > 4)
  123. $output .= "<a href=\"". remove_query_arg('paged' ) . "\" title=\"" . __('First Page', 'wpsc') . "\">" . __('&laquo; First', 'wpsc') . "</a>";
  124.  
  125. // Should we show the PREVIOUS PAGE link?
  126. if($current_page > 1) {
  127. $previous_page = $current_page - 1;
  128. if( $previous_page == 1 )
  129. $output .= " <a href=\"". remove_query_arg( 'paged' ) . $additional_links . "\" title=\"" . __('Previous Page', 'wpsc') . "\">" . __('&lt; Previous', 'wpsc') . "</a>";
  130. else
  131. $output .= " <a href=\"". add_query_arg( 'paged', ($current_page - 1) ) . $additional_links . "\" title=\"" . __('Previous Page', 'wpsc') . "\">" . __('&lt; Previous', 'wpsc') . "</a>";
  132. }
  133. $i =$current_page - $num_paged_links;
  134. $count = 1;
  135. if($i <= 0) $i =1;
  136. while($i < $current_page){
  137. if($count <= $num_paged_links){
  138. if($i == 1)
  139. $output .= " <a href=\"". remove_query_arg('paged' ) . "\" title=\"" . sprintf( __('Page %s', 'wpsc'), $i ) . " \">".$i."</a>";
  140. else
  141. $output .= " <a href=\"". add_query_arg('paged', $i ) . "\" title=\"" . sprintf( __('Page %s', 'wpsc'), $i ) . " \">".$i."</a>";
  142. }
  143. $i++;
  144. $count++;
  145. }
  146. // Current Page Number
  147. if($current_page > 0)
  148. $output .= "<span class='current'>$current_page</span>";
  149.  
  150. //Links after Current Page
  151. $i = $current_page + $num_paged_links;
  152. $count = 1;
  153.  
  154. if($current_page < $totalpages){
  155. while(($i) > $current_page){
  156.  
  157. if($count < $num_paged_links && ($count+$current_page) <= $totalpages){
  158. $output .= " <a href=\"". add_query_arg( 'paged', ($count+$current_page) ) . "\" title=\"" . sprintf( __('Page %s', 'wpsc'), ($count+$current_page) ) . "\">".($count+$current_page)."</a>";
  159. $i++;
  160. }else{
  161. break;
  162. }
  163. $count ++;
  164. }
  165. }
  166.  
  167. if($current_page < $totalpages) {
  168. $next_page = $current_page + 1;
  169. $output .= "<a href=\"". add_query_arg( 'paged', $next_page ) . "\" title=\"" . __('Next Page', 'wpsc') . "\">" . __('Next &gt;', 'wpsc') . "</a>";
  170. }
  171. // Should we show the LAST PAGE link?
  172. if($current_page < $totalpages) {
  173. $output .= "<a href=\"". add_query_arg( 'paged', $totalpages ) . "\" title=\"" . __('Last Page', 'wpsc') . "\">" . __('Last &raquo;', 'wpsc') . "</a>";
  174. }
  175. }
  176. // Return the output.
  177. echo $output;
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement