首页 文章

显示每种产品下的产品类别

提问于
浏览
0

我正在尝试在商店页面的产品图片下显示每个产品类别 .

Here's the current page

Here's where I want to display the category name

我已尝试使用以下代码,但它似乎不能从functions.php文件中获取当前的产品类别 .

And here it comes...

function sv_add_text_under_wc_shop_image() {

    echo $product->get_categories( ', ', '<span class="posted_in">' . _n( 'Category:', 'Categories:', sizeof( get_the_terms( $post->ID, 'product_cat' ) ) ) . ' ', '</span>' );
}

add_action( 'woocommerce_before_shop_loop_item_title', 'sv_add_text_under_wc_shop_image', 10 );

1 回答

  • 0

    将content-product.php的副本复制到您的子主题并找到以下行:

    <h3><?php the_title(); ?></h3>
    

    在它之后添加:

    <?php
            $terms = get_the_terms($post->ID,'product_cat');
            $count = count($terms); $i=0;
            if ($count > 0) {
                $term_list = '<div class="parent_category">';
                foreach ($terms as $term) {
                    $i++;
                    if ($term->parent==0) {
                        $term_list .= '<a href="/?product_cat=' . $term->slug . '">' . $term->name . '</a>';
                        if ($count != $i) $term_list .= ' &middot; ';
                    }
                }
                echo $term_list .'</div>';
            }
    ?>
    

相关问题