Based on the answer to my last question, I have a loop on my product-archive.php
that displays products by category; however, because my shop page uses archive-product.php and post_type=product, the custom category loop doesn't work on the shop page specifically. How can I have multiple loops on archive.php dependent on different conditions?
I haven't found the same question here yet. Based on this documentation, I'm using is_shop for the shop page loop, and is_product_category() for the category loop.
This is my archive-product.php but it results in the shop page loop not displaying any products:
<?php
if ( woocommerce_product_loop() ) {
/**
* Hook: woocommerce_before_shop_loop.
*
* @hooked woocommerce_output_all_notices - 10
* @hooked woocommerce_result_count - 20
* @hooked woocommerce_catalog_ordering - 30
*/
do_action( 'woocommerce_before_shop_loop' ); ?>
<ul class="wpf-search-container products">
<li class="product">
<?php
// Get The queried object ( a WP_Term or a WP_Post Object)
$term = get_queried_object();
// To be sure that is a WP_Term Object to avoid errors
if( is_a($term, 'WP_Term') ) :
if ( is_product_category() ) :
// Setup your custom query
$loop = new WP_Query( array(
'post_type' => 'product',
'posts_per_page' => -1,
'post_status' => 'publish',
'tax_query' => array( array(
'taxonomy' => 'product_cat', // The taxonomy name
'field' => 'term_id', // Type of field ('term_id', 'slug', 'name' or 'term_taxonomy_id')
'terms' => $term->term_id, // can be an integer, a string or an array
) ),
) );
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post();
the_post_thumbnail( 'thumbnail');
endwhile;
wp_reset_postdata(); // Remember to reset
endif; endif;
?>
<?php
if ( is_shop() ) :
// Setup your custom query
$loop = new WP_Query( array(
'post_type' => 'product',
'posts_per_page' => -1,
'post_status' => 'publish',
) );
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post();
the_post_thumbnail( 'thumbnail');
endwhile;
wp_reset_postdata(); // Remember to reset
endif; endif; endif;
?>
</li>
</ul>
Aucun commentaire:
Enregistrer un commentaire