function wpsc_pagination($totalpages = '', $per_page = '', $current_page = '', $page_link = '') {
global $wp_query;
$num_paged_links = 4; //amount of links to show on either side of current page
$additional_links = '';
//additional links, items per page and products order
if( get_option('permalink_structure') != '' ){
$additional_links_separator = '?';
}else{
$additional_links_separator = '&';
}
if( !empty( $_GET['items_per_page'] ) ){
$additional_links = $additional_links_separator . 'items_per_page=' . $_GET['items_per_page'];
$additional_links_separator = '&';
}
if( !empty( $_GET['product_order'] ) )
$additional_links .= $additional_links_separator . 'product_order=' . $_GET['product_order'];
$additional_links = apply_filters('wpsc_pagination_additional_links', $additional_links);
//end of additional links
if(empty($totalpages)){
$totalpages = $wp_query->max_num_pages;
}
if(empty($per_page))
$per_page = (int)get_option('wpsc_products_per_page');
$current_page = absint( get_query_var('paged') );
if($current_page == 0)
$current_page = 1;
if(empty($page_link))
$page_link = wpsc_a_page_url();
//if there is no pagination
if(!get_option('permalink_structure')) {
$category = '?';
if(isset($wp_query->query_vars['wpsc_product_category']))
$category = '?wpsc_product_category='.$wp_query->query_vars['wpsc_product_category'];
if(isset($wp_query->query_vars['wpsc_product_category']) && is_string($wp_query->query_vars['wpsc_product_category'])){
$page_link = get_option('blogurl').$category.'&paged';
}else{
$page_link = get_option('product_list_url').$category.'&paged';
}
$separator = '=';
}else{
if ( isset( $wp_query->query_vars['wpsc_product_category'] ) ) {
$category_id = get_term_by( 'slug', $wp_query->query_vars['wpsc_product_category'], 'wpsc_product_category' );
$page_link = trailingslashit( get_term_link( $category_id, 'wpsc_product_category' ) );
} else {
$page_link = trailingslashit( get_option( 'product_list_url' ) );
}
$separator = 'page/';
}
// If there's only one page, return now and don't bother
if($totalpages == 1)
return;
// Pagination Prefix
$output = __('Pages: ','wpsc');
if(get_option('permalink_structure')){
// Should we show the FIRST PAGE link?
if($current_page > 2)
$output .= "<a href=\"". esc_url( $page_link . $additional_links ) . "\" title=\"" . __('First Page', 'wpsc') . "\">" . __('« First', 'wpsc') . "</a>";
// Should we show the PREVIOUS PAGE link?
if($current_page > 1) {
$previous_page = $current_page - 1;
if( $previous_page == 1 )
$output .= " <a href=\"". esc_url( $page_link . $additional_links ) . "\" title=\"" . __('Previous Page', 'wpsc') . "\">" . __('< Previous', 'wpsc') . "</a> /";
else
$output .= " <a href=\"". esc_url( $page_link .$separator. $previous_page . $additional_links ) . "\" title=\"" . __('Previous Page', 'wpsc') . "\">" . __('< Previous', 'wpsc') . "</a> /";
}
$i =$current_page - $num_paged_links;
$count = 1;
if($i <= 0) $i =1;
while($i < $current_page){
if($count <= $num_paged_links){
if($count == 1)
$output .= "<a href=\"". esc_url( $page_link . $additional_links ) . "\" title=\"" . sprintf( __('Page %s', 'wpsc'), $i ) . " \">".$i."</a>";
else
$output .= " <a href=\"". esc_url( $page_link .$separator. $i . $additional_links ) . "\" title=\"" . sprintf( __('Page %s', 'wpsc'), $i ) . " \">".$i."</a>";
}
$i++;
$count++;
}
// Current Page Number
if($current_page > 0)
$output .= "<span class='current'>$current_page</span>";
//Links after Current Page
$i = $current_page + $num_paged_links;
$count = 1;
if($current_page < $totalpages){
while(($i) > $current_page){
if($count < $num_paged_links && ($count+$current_page) <= $totalpages){
$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>";
$i++;
}else{
break;
}
$count ++;
}
}
if($current_page < $totalpages) {
$next_page = $current_page + 1;
$output .= "<span>/</span><a href=\"". esc_url( $page_link .$separator. $next_page . $additional_links ) . "\" title=\"" . __('Next Page', 'wpsc') . "\">" . __('Next >', 'wpsc') . "</a>";
}
// Should we show the LAST PAGE link?
if($current_page < $totalpages - 3) {
$output .= "<a href=\"". esc_url( $page_link .$separator. $totalpages . $additional_links ) . "\" title=\"" . __('Last Page', 'wpsc') . "\">" . __('Last »', 'wpsc') . "</a>";
}
} else {
// Should we show the FIRST PAGE link?
if($current_page > 4)
$output .= "<a href=\"". remove_query_arg('paged' ) . "\" title=\"" . __('First Page', 'wpsc') . "\">" . __('« First', 'wpsc') . "</a>";
// Should we show the PREVIOUS PAGE link?
if($current_page > 1) {
$previous_page = $current_page - 1;
if( $previous_page == 1 )
$output .= " <a href=\"". remove_query_arg( 'paged' ) . $additional_links . "\" title=\"" . __('Previous Page', 'wpsc') . "\">" . __('< Previous', 'wpsc') . "</a>";
else
$output .= " <a href=\"". add_query_arg( 'paged', ($current_page - 1) ) . $additional_links . "\" title=\"" . __('Previous Page', 'wpsc') . "\">" . __('< Previous', 'wpsc') . "</a>";
}
$i =$current_page - $num_paged_links;
$count = 1;
if($i <= 0) $i =1;
while($i < $current_page){
if($count <= $num_paged_links){
if($i == 1)
$output .= " <a href=\"". remove_query_arg('paged' ) . "\" title=\"" . sprintf( __('Page %s', 'wpsc'), $i ) . " \">".$i."</a>";
else
$output .= " <a href=\"". add_query_arg('paged', $i ) . "\" title=\"" . sprintf( __('Page %s', 'wpsc'), $i ) . " \">".$i."</a>";
}
$i++;
$count++;
}
// Current Page Number
if($current_page > 0)
$output .= "<span class='current'>$current_page</span>";
//Links after Current Page
$i = $current_page + $num_paged_links;
$count = 1;
if($current_page < $totalpages){
while(($i) > $current_page){
if($count < $num_paged_links && ($count+$current_page) <= $totalpages){
$output .= " <a href=\"". add_query_arg( 'paged', ($count+$current_page) ) . "\" title=\"" . sprintf( __('Page %s', 'wpsc'), ($count+$current_page) ) . "\">".($count+$current_page)."</a>";
$i++;
}else{
break;
}
$count ++;
}
}
if($current_page < $totalpages) {
$next_page = $current_page + 1;
$output .= "<a href=\"". add_query_arg( 'paged', $next_page ) . "\" title=\"" . __('Next Page', 'wpsc') . "\">" . __('Next >', 'wpsc') . "</a>";
}
// Should we show the LAST PAGE link?
if($current_page < $totalpages) {
$output .= "<a href=\"". add_query_arg( 'paged', $totalpages ) . "\" title=\"" . __('Last Page', 'wpsc') . "\">" . __('Last »', 'wpsc') . "</a>";
}
}
// Return the output.
echo $output;
}