问题描述
基于对上一个问题的回答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 (将#修改为@)