我最近设置了产品以在其实际标题中显示品牌。在上面的示例中,“散装商品”被视为品牌。现在,我将其放入了产品标题中,我想将其从上面的较小文本实现中删除,而是拥有该空间显示类别。特别是第二层类别。我所有的产品至少有两个类别,所以我希望第二个出现在这里。
这是我以前一直在使用标题之前显示该品牌的代码:
add_action( 'woocommerce_before_shop_loop_item_title', 'aq_display_brand_before_title' );
function aq_display_brand_before_title(){
global $product;
$taxonomy = 'product_brand'; // <= Here we set the correct taxonomy
$brand_terms = get_the_terms( $product->get_id(), $taxonomy );
// Check if it's the correct taxonomy and if there are terms assigned to the product
if ( $brand_terms && ! is_wp_error( $brand_terms ) ) {
// Loop through assigned terms
foreach( $brand_terms as $term ) {
if ( is_a($term, 'WP_Term') ) {
echo '<a href="' . get_term_link( $term ) . '">' . $term->name . '</a>';
}
}
}
}
在您的代码中,您需要将分类法替换为从
product_brand
替换为
product_cat
add_action( 'woocommerce_before_shop_loop_item_title', 'aq_display_2nd_category_before_title' );
function aq_display_2nd_category_before_title(){
global $product;
$taxonomy = 'product_cat'; // <= Here we set the correct taxonomy
$cat_terms = get_the_terms( $product->get_id(), $taxonomy );
// Check if it's the correct taxonomy and if there are terms assigned to the product
if ( $cat_terms && ! is_wp_error( $cat_terms ) ) {
// Get the last category term
$term = end($cat_terms);
if ( is_a($term, 'WP_Term') ) {
echo '<a href="' . get_term_link( $term ) . '">' . $term->name . '</a>';
}
}
}