在商店页面上按品牌显示/排序产品-Woocommerce

时间:2020-02-28 03:51:04

标签: woocommerce

我想在商店页面上按品牌显示或分类产品。 像this一样,我尝试使用此代码使用archive-product.php

但是仍然没有运气,我们将不胜感激任何建议或帮助。 谢谢!

<?php
    $categories_args = array(
        'taxonomy' => 'pwb-brand'  
    );
    $product_categories = get_terms( $categories_args );  
    if ($product_categories) { 
        echo '<ul class="catalog">';
        foreach ($product_categories as $product_category) {
            $term_id    = $product_category->term_id; 
            $term_name  = $product_category->name; 
            $term_desc  = $product_category->description;  
            $term_link  = get_term_link($product_category->slug, $product_category->taxonomy);  
            $thumbnail_id = get_woocommerce_term_meta( $product_category->term_id, 'thumbnail_id', true );
            $image = wp_get_attachment_image_url( $thumbnail_id );  
            echo '<li class="product-cat-'.$term_id.'">'; 
            echo '<h4 class="product-cat-title"><a href="'.$term_link.'">'.$term_name.'</a></h4>';  
            echo '<img src="' . $image . '" alt="" style="width:120px;height:120px;"/>';
            echo '<p class="product-cat-description">'.$term_desc .'</p>';  
            $products_args = array(
                'post_type'     => 'product',  
                'post_per_page' => 1,
                'tax_query'     => array( 
                    array(
                        'taxonomy' => 'pwb-brand',
                        'field'    => 'term_id',
                        'terms'    => $term_id,  
                    ),
                ),
            );
            $products = new WP_Query( $products_args );
            if ( $products->have_posts() ) { 
                woocommerce_product_loop_start();
                while ( $products->have_posts() ) : $products->the_post();
                    wc_get_template_part( 'content', 'product' );
                endwhile;  
                woocommerce_product_loop_end();
                wp_reset_postdata();
            } else {  

                wc_get_template( 'loop/no-products-found.php' );
            } 
            echo '</li>'; 
        }
        echo '</ul>'; 
    } 
?>

0 个答案:

没有答案