WooCommerce自定义产品循环问题求助:重复循环同一产品
Hey there, let's work through this repeating product loop issue you're hitting in WooCommerce. From the code snippet you shared, here are the most likely fixes to get your loop running correctly:
1. Double-check your query parameters
More often than not, repeating loops happen because your custom query isn't properly limited or paginated.
- If you're using
WP_Queryfor your product loop, make sure you set clear limits and pagination:$args = array( 'post_type' => 'product', 'posts_per_page' => 12, // Adjust to your desired number of products per page 'paged' => get_query_var('paged') ?: 1, // Handles pagination correctly ); $custom_product_query = new WP_Query($args); - If you're using WooCommerce's native
WC_Product_Query, use similar parameters:$args = array( 'limit' => 12, 'paginate' => true, 'paged' => get_query_var('paged') ?: 1, ); $product_query = new WC_Product_Query($args);
2. Always reset your query after the loop
This is a critical step most people miss. If you create a custom query, WordPress will hold onto that query context unless you reset it—leading to repeated product data.
Add this right after your loop ends:
wp_reset_postdata();
Here's how it fits into a full loop example:
<?php $args = array( 'post_type' => 'product', 'posts_per_page' => 12, 'paged' => get_query_var('paged') ?: 1, ); $custom_query = new WP_Query($args); if ($custom_query->have_posts()) : while ($custom_query->have_posts()) : $custom_query->the_post(); // Your product template code (e.g., wc_get_template_part('content', 'product')) wc_get_template_part('content', 'product'); endwhile; wp_reset_postdata(); // Don't skip this! else : echo '<p>No products found.</p>'; endif; ?>
3. Check for duplicate hook calls
From your code snippet, you're removing default WooCommerce hooks like woocommerce_catalog_ordering—make sure you aren't accidentally calling the product loop hook multiple times. For example, avoid repeating do_action('woocommerce_product_loop') or reinitializing the loop without clearing the previous context.
4. Avoid re-querying products inside the loop
If you're running a secondary query (like wc_get_product() or get_post()) inside your loop without referencing the current post correctly, it can force the loop to stick to the same product. Instead, use the global product object or pull the product ID directly from the loop's current post:
global $product; // Or if you need to reinitialize: $product = wc_get_product(get_the_ID());
5. Verify pagination is working
If your paged parameter isn't being passed correctly, your query will keep loading the first page of products over and over. Test this by adding var_dump(get_query_var('paged')) above your loop—if it returns 0 or null, adjust your parameter to fall back to 1 like in the examples above.
If none of these fix the issue, sharing your full loop code would help narrow down the exact problem, but these steps cover 90% of repeating loop cases in WooCommerce.
内容的提问来源于stack exchange,提问作者Aurimas Gelžinis

