admin管理员组文章数量:1314496
My WooCommerce store is set up so that a product brand is displaying above the title, as in the screenshot below:
I've more recently set up products to display brand in their actual title. In the example above, "Bulk Goods" is set as the brand. Now that I've put it in product title, I'd like to remove it from the smaller text implementation above, and instead have that space show category. In particular, the second-tier category. All of my products have at least two categories, so I'd like the second one to appear here.
This is the code that I have been using previously to display the brand before the title:
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>';
}
}
}
}
My WooCommerce store is set up so that a product brand is displaying above the title, as in the screenshot below:
I've more recently set up products to display brand in their actual title. In the example above, "Bulk Goods" is set as the brand. Now that I've put it in product title, I'd like to remove it from the smaller text implementation above, and instead have that space show category. In particular, the second-tier category. All of my products have at least two categories, so I'd like the second one to appear here.
This is the code that I have been using previously to display the brand before the title:
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>';
}
}
}
}
Share
Improve this question
edited Jan 30 at 13:55
LoicTheAztec
254k24 gold badges397 silver badges443 bronze badges
asked Jan 30 at 13:15
Graham ToomeyGraham Toomey
254 bronze badges
0
1 Answer
Reset to default 1In your code, you need to replace the taxonomy from product_brand
to product_cat
, then you can get and display the 2nd category like:
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>';
}
}
}
It should work.
本文标签: phpReplace the brand with a different category above product in WooCommerce catalogStack Overflow
版权声明:本文标题:php - Replace the brand with a different category above product in WooCommerce catalog - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741962620a2407370.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论