WooCommerce:如何在单 product.php 中显示类别名称
WooCommerce: How to display Category Name in single-product.php
如何在单机中显示分类名称-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?
在存档中-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' );
}