如何在single-product.php中显示类别名称?
在archive-product.php中,代码为:
<?php woocommerce_page_title(); ?>
但我可以用什么来显示属于该类别的single-product.php中的类别名称?
答案 0 :(得分:2)
试试这个:
global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($terms as $term) {
echo $term->name .' ';
$thumbnail_id = get_woocommerce_term_meta( $term->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );
echo "'{$image}'";
}
答案 1 :(得分:0)
在content-single-product-CATEGORYNAME.php
woocommerce_get_template_part( 'content', 'single-product' );
将其更改为:
if (is_product_category( 'CATEGORYNAME' ) {
woocommerce_get_template_part( 'content', 'single-product-CATEGORYNAME' );
}else{
woocommerce_get_template_part( 'content', 'single-product' );
}