有人可以帮我找到完美的解决方案吗?我的客户需要在产品类别页面上只显示相关品牌的产品。就像我有产品类别护发的页面,它们都是与护发相关的所有产品,但品牌不同,目前我使用小部件在产品类别页面上显示所有品牌,但他只需要相关品牌的产品
我试过这段代码,但什么也没发生
函数 custom_product_brands_test() {
$category = get_queried_object();
$current_tax = get_taxonomy($category->taxonomy );
$args = array(
'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' =>$current_tax->labels->singular_name,
'field' => 'term_id',
'terms' => $category->term_id,
),
),
'posts_per_page' => -1,
);
$products = get_posts( $args );
if (is_product_category()) {
$category = get_queried_object();
$args = array(
'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $category->term_id,
),
),
'posts_per_page' => -1,
);
$products = get_posts( $args );
$brands = array();
foreach ( $products as $product ) {
$product_brands = wp_get_post_terms( $product->ID, 'items');
foreach ( $product_brands as $product_brand ) {
$brands[ $product_brand->term_id ] = $product_brand->name;
}
}
if ( ! empty( $brands ) ) {
echo '<ul>';
foreach ( $brands as $brand ) {
echo '<li>' . $brand . '</li>';
}
echo '</ul>';
}
}
}
add_action( 'woocommerce_before_main_content', 'custom_product_brands_test', 10 );