如何解决如何根据不同条件在archive.php上具有多个循环
基于对上一个问题的回答product-archive.php
模板文件上有一个循环,该循环按类别显示产品。
但是,由于我的商店页面使用archive-product.php
模板文件和post_type=product
,因此自定义类别循环特别不适用于商店页面。如何根据不同的条件在archive.php上进行多个循环?
我在这里还没有找到相同的问题。基于this documentation,我在商店页面循环中使用is_shop,在类别循环中使用is_product_category()
。
这是我的archive-product.php
模板文件,但导致商店页面循环中未显示任何产品:
<?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',) );
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>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。