Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //My problem with pagination:
- //First I have this category template (archive-products.php, from woocommerce):
- <?php
- /**
- * The Template for displaying product archives, including the main shop page which is a post type archive.
- *
- * Override this template by copying it to yourtheme/woocommerce/archive-product.php
- *
- * @author WooThemes
- * @package WooCommerce/Templates
- * @version 2.0.0
- */
- if (!defined('ABSPATH')) {
- exit; // Exit if accessed directly
- }
- get_header('shop'); ?>
- <?php
- /**
- * woocommerce_before_main_content hook
- *
- * @hooked woocommerce_output_content_wrapper - 10 (outputs opening divs for the content)
- * @hooked woocommerce_breadcrumb - 20
- */
- do_action('woocommerce_before_main_content');
- ?>
- <?php if (apply_filters('woocommerce_show_page_title', true)) : ?>
- <h1 class="page-title" <?php echo !is_singular() && (is_tax('product_cat') || is_tax('product_tag')) ? 'style="display:block"' : ''; ?>><?php woocommerce_page_title(); ?></h1>
- <?php endif; ?>
- <?php do_action('woocommerce_archive_description'); ?>
- <?php if (have_posts()) :
- global $wp_query; // I call $wp_query so I could change posts_found attribute, so pagination would be recalculated with new results
- /*echo '<pre>'; // This is for correct handling of newlines
- ob_start();
- var_dump($wp_query->query_vars['posts_per_page']); //20
- $a=ob_get_contents();
- ob_end_clean();
- echo htmlspecialchars($a,ENT_QUOTES); // Escape every HTML special chars (especially > and < )
- echo '</pre>';*/
- /**
- * woocommerce_before_shop_loop hook
- *
- * @hooked woocommerce_result_count - 20
- * @hooked woocommerce_catalog_ordering - 30
- */
- do_action('woocommerce_before_shop_loop');
- echo $wp_query->found_posts;
- woocommerce_product_loop_start();
- woocommerce_product_subcategories();
- global $product_categories_global_total; // Get the total number of subcategories in a category
- $i = 0;
- while (have_posts()) : the_post();
- if ($wp_query->queried_object->term_id == wp_get_post_terms(get_the_ID(), 'product_cat')[0]->term_id):
- wc_get_template_part('content', 'product');
- $i++;
- endif;
- endwhile; // end of the loop.
- $wp_query->found_posts = $product_categories_global_total+$i; // I get this one from customized subcategories function Look at the function below this template
- woocommerce_product_loop_end();
- /**
- * woocommerce_after_shop_loop hook
- *
- * @hooked woocommerce_pagination - 10
- */
- do_action('woocommerce_after_shop_loop');
- elseif (!woocommerce_product_subcategories(array('before' => woocommerce_product_loop_start(false), 'after' => woocommerce_product_loop_end(false)))) : ?>
- <?php wc_get_template('loop/no-products-found.php'); ?>
- <?php endif; ?>
- <?php
- /**
- * woocommerce_after_main_content hook
- *
- * @hooked woocommerce_output_content_wrapper_end - 10 (outputs closing divs for the content)
- */
- do_action('woocommerce_after_main_content');
- ?>
- <?php
- /**
- * woocommerce_sidebar hook
- *
- * @hooked woocommerce_get_sidebar - 10
- */
- do_action('woocommerce_sidebar');
- ?>
- <?php get_footer('shop'); ?>
- // The customized subcategories function
- $product_categories_global_total = 0;
- function woocommerce_product_subcategories( $args = array() ) {
- global $woocommerce, $wp_query, $_chosen_attributes;
- $defaults = array(
- 'before' => '',
- 'after' => '',
- 'force_display' => false
- );
- $args = wp_parse_args( $args, $defaults );
- extract( $args );
- // Main query only
- if ( ! is_main_query() && ! $force_display ) return;
- // Don't show when filtering
- if ( sizeof( $_chosen_attributes ) > 0 || ( isset( $_GET['max_price'] ) && isset( $_GET['min_price'] ) ) ) return;
- // Don't show when searching or when on page > 1 and ensure we're on a product archive
- if ( is_search() || is_paged() || ( ! is_product_category() && ! is_shop() ) ) return;
- // Check cateogries are enabled
- if ( is_product_category() && get_option( 'woocommerce_show_subcategories' ) == 'no' ) return;
- if ( is_shop() && get_option( 'woocommerce_shop_show_subcategories' ) == 'no' ) return;
- // Find the category + category parent, if applicable
- if ( $product_cat_slug = get_query_var( 'product_cat' ) ) {
- $product_cat = get_term_by( 'slug', $product_cat_slug, 'product_cat' );
- $product_category_parent = $product_cat->term_id;
- } else {
- $product_category_parent = 0;
- }
- // NOTE: using child_of instead of parent - this is not ideal but due to a WP bug ( http://core.trac.wordpress.org/ticket/15626 ) pad_counts won't work
- $args = array(
- 'child_of' => $product_category_parent,
- 'menu_order' => 'ASC',
- 'hide_empty' => 0,
- 'hierarchical' => 1,
- 'taxonomy' => 'product_cat',
- 'pad_counts' => 1
- );
- $product_categories = get_categories( $args );
- $product_category_found = false;
- if ( $product_categories ) {
- global $product_categories_global_total;
- foreach ( $product_categories as $category ) {
- if ( $category->parent != $product_category_parent ){
- continue;
- } else {
- $product_categories_global_total++;
- }
- if ( ! $product_category_found ) {
- // We found a category
- $product_category_found = true;
- echo $before;
- }
- wc_get_template( 'content-product_cat.php', array(
- 'category' => $category
- ) );
- }
- }
- // If we are hiding products disable the loop and pagination
- if ( $product_category_found == true && get_option( 'woocommerce_hide_products_when_showing_subcategories' ) == 'yes' ) {
- $wp_query->post_count = 0;
- $wp_query->max_num_pages = 0;
- }
- if ( $product_category_found ) {
- echo $after;
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment