在您的代码中,您需要将分类法替换为从
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>';
}
}
}
应该起作用