WooCommerce:如何在single-product.php中显示类别名称
问题描述:
如何在single-product.php中显示类别名称?WooCommerce:如何在single-product.php中显示类别名称
在归档product.php代码:
<?php woocommerce_page_title(); ?>
,但我能怎么使用属于该类别单product.php显示类别名称?
答
试试这个:
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}'";
}
答
找到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');
}
它的工作原理,谢谢 –
在归档product.php我用了一个背景与类别图像(使用此方法http://stackoverflow.com/questions/30918268/how-can-i-set-the-image-of -woocommerce-category-as-the-background-of-the-categor),是否可以在single-product.php中使用类别图像? –
是的,你可以使用该代码,你有termid,所以你可以得到缩略图。 –