Last active
August 29, 2015 14:13
-
-
Save bappi-d-great/b4ab43b902c0c1552e70 to your computer and use it in GitHub Desktop.
Custom Pagination for WPMU Marketpress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| add_filter( 'mp_products_nav', 'mp_products_nav_cb', 99, 2 ); | |
| function mp_products_nav_cb( $html, $custom_query ){ | |
| global $wp_query, $mp; | |
| $args = $mp->parse_args(func_get_args(), $mp->defaults['list_products']); | |
| $args['nopaging'] = false; | |
| $query = array( | |
| 'post_type' => 'product', | |
| 'post_status' => 'publish', | |
| ); | |
| $tax_query = array(); | |
| //setup taxonomies if possible | |
| if ( $wp_query->get('taxonomy') == 'product_category' ) { | |
| $tax_query[] = array( | |
| 'taxonomy' => 'product_category', | |
| 'field' => 'slug', | |
| 'terms' => $wp_query->get('term'), | |
| ); | |
| } elseif ( $wp_query->get('taxonomy') == 'product_tag' ) { | |
| $tax_query[] = array( | |
| 'taxonomy' => 'product_tag', | |
| 'field' => 'slug', | |
| 'terms' => $wp_query->get('term'), | |
| ); | |
| } elseif ( !is_null($args['category']) || !is_null($args['tag']) ) { | |
| if ( !is_null($args['category']) ) { | |
| $tax_query[] = array( | |
| 'taxonomy' => 'product_category', | |
| 'field' => 'slug', | |
| 'terms' => sanitize_title($args['category']), | |
| ); | |
| } | |
| if ( !is_null($args['tag']) ) { | |
| $tax_query[] = array( | |
| 'taxonomy' => 'product_tag', | |
| 'field' => 'slug', | |
| 'terms' => sanitize_title($args['tag']), | |
| ); | |
| } | |
| } | |
| if ( count($tax_query) > 1 ) { | |
| $query['tax_query'] = array_merge(array('relation' => 'AND'), $tax_query); | |
| } elseif ( count($tax_query) == 1 ) { | |
| $query['tax_query'] = $tax_query; | |
| } | |
| //setup pagination | |
| if ( (!is_null($args['paginate']) && !$args['paginate']) || (is_null($args['paginate']) && !$mp->get_setting('paginate')) ) { | |
| $query['nopaging'] = $args['nopaging'] = true; | |
| } else { | |
| //figure out perpage | |
| $query['posts_per_page'] = -1; | |
| //figure out page | |
| if ( !is_null($args['page']) ) { | |
| $query['paged'] = intval($args['page']); | |
| } elseif ( $wp_query->get('paged') != '' ) { | |
| $query['paged'] = $args['page'] = intval($wp_query->get('paged')); | |
| } | |
| //get order by | |
| if ( is_null($args['order_by']) ) { | |
| if ( 'price' == $mp->get_setting('order_by') ) { | |
| $query['meta_key'] = 'mp_price_sort'; | |
| $query['orderby'] = 'meta_value_num'; | |
| } elseif ( 'sales' == $mp->get_setting('order_by') ) { | |
| $query['meta_key'] = 'mp_sales_count'; | |
| $query['orderby'] = 'meta_value_num'; | |
| } else { | |
| $query['orderby'] = $mp->get_setting('order_by'); | |
| } | |
| } else { | |
| if ( 'price' == $args['order_by'] ) { | |
| $query['meta_key'] = 'mp_price_sort'; | |
| $query['orderby'] = 'meta_value_num'; | |
| } else if ( 'sales' == $args['order_by'] ) { | |
| $query['meta_key'] = 'mp_sales_count'; | |
| $query['orderby'] = 'meta_value_num'; | |
| } else { | |
| $query['orderby'] = $args['order_by']; | |
| } | |
| } | |
| } | |
| //get order direction | |
| if ( is_null($args['order']) ) { | |
| $query['order'] = $mp->get_setting('order'); | |
| } else { | |
| $query['order'] = $args['order']; | |
| } | |
| //The Query | |
| $my_query = new WP_Query($query); | |
| $tot = count($my_query->posts); | |
| $page = max(1, $custom_query->get('paged')); | |
| $max = $custom_query->max_num_pages; | |
| $prev=$next=$html=$mid=''; | |
| $flag = 3; | |
| if ($max > 1) { | |
| if ($page != 1) { | |
| $html.='<a href="#page='.($page-1).'">'.__('Prev').'</a>'; | |
| } | |
| $j = 0; | |
| $dot = 0; | |
| $a = $b = 0; | |
| for( $i = 0; $i < $tot; $i += $mp->get_setting('per_page') ){ | |
| if( $j < $flag ){ | |
| $html.= '<a ' . ( $page == $j+1 ? 'class="activePage"' : '' ) . ' href="#page='.(++$j).'">'.$j.'</a>'; | |
| }elseif( $tot > $j && $j >= ( ( $tot / $mp->get_setting('per_page') ) - $flag ) ){ | |
| $html.= '<a ' . ( $page == $j+1 ? 'class="activePage"' : '' ) . ' href="#page='.(++$j).'">'.$j.'</a>'; | |
| } | |
| else{ | |
| if( ($page - 2) <= $j && $j <= ($page) ){ | |
| $html.= '<a ' . ( $page == $j+1 ? 'class="activePage"' : '' ) . ' href="#page='.(++$j).'">'.$j.'</a>'; | |
| }else{ | |
| if( $j > $flag ){ | |
| if( $a++ == 0 ) | |
| $html .= '...'; | |
| } | |
| if( $j > ( $page + 2 ) ){ | |
| if( $b++ == 0 ) | |
| $html .= '...'; | |
| } | |
| $j++; | |
| } | |
| } | |
| } | |
| $i = $j = 0; | |
| if ( $page != $max ) { | |
| $html.='<a href="#page='.($page+1).'">'.__('Next').'</a>'; | |
| } | |
| $html = '<div id="mp_product_nav">' . $html . '</div>'; | |
| } | |
| return $html; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can add those codes in your functions.php in the theme, if you think your theme won’t be changed. Otherwise mu-plugins is the best solution. To use mu-plugins, go to /wp-content/ and find the folder with name 'mu-plugins'. If there is no folder in that name, then create a folder, name it 'mu-plugins', create a file inside that, give any name you like and paste the code in there. You don't need to activate that plugin. Mu-plugins means must use plugins, so it will be activated automatically always. If you use mu-plugins then add a php start tag at the beginning of the code.