I want to display a different description for a post if it belongs to a specific product category in a filter.
I've used the code below code to add the code but it is displaying for all products:
function herara_filter_short_description( $desc ){
global $product;
if ( is_single( $product->id ) ) {
$desc = '<span class="sku_wrapper">SKU: <span class="sku">'. $product->get_sku() .'</span></span></BR><span class="material_wrapper">MATERIAL: <span class="material">'. $product->get_sku() .'</span></span>';
}
return $desc;
}
add_filter( 'woocommerce_short_description', 'herara_filter_short_description' );
I want to display a different description for a post if it belongs to a specific product category in a filter.
I've used the code below code to add the code but it is displaying for all products:
function herara_filter_short_description( $desc ){
global $product;
if ( is_single( $product->id ) ) {
$desc = '<span class="sku_wrapper">SKU: <span class="sku">'. $product->get_sku() .'</span></span></BR><span class="material_wrapper">MATERIAL: <span class="material">'. $product->get_sku() .'</span></span>';
}
return $desc;
}
add_filter( 'woocommerce_short_description', 'herara_filter_short_description' );
Share
Improve this question
edited Oct 5, 2020 at 11:03
Tom J Nowell♦
61k7 gold badges79 silver badges148 bronze badges
asked Oct 5, 2020 at 10:38
Ahmad DarwishAhmad Darwish
12 bronze badges
5
|
1 Answer
Reset to default 0You can test if a post has a particular term assigned to it with the has_term
function.
E.g.
if ( has_term( 'term-slug', 'taxonomy-slug' ) ) {
// do stuff
}
With this you can test if your "post" has a particular "term" ( aka if your "product
" has a particular "product_cat
" )
product
) has a particular term in a taxonomy? ( where the taxonomy is the woocommerce product category taxonomy? ). How to test if a post has a term in a taxonomy is a much better question that achieves the same thing and is 100x easier to find an answer for. – Tom J Nowell ♦ Commented Oct 5, 2020 at 11:02is_single
was false thennull
would be returned, overwriting any other filters, so I changed$new_desc
to$desc
. It's also not necessary to declare the$product
global and pass its ID, the current post is already set to that product so it already knows which post to check – Tom J Nowell ♦ Commented Oct 5, 2020 at 11:07product
, much like pages are posts of typepage
, products are just another custom post type. Much like categories and tags are taxonomies, so are product categories, under the hood they're all the same. ( note that if they were not, then this Q would be off topic, 3rd party plugin dev support is offtopic here so your question would have been closed ) – Tom J Nowell ♦ Commented Oct 5, 2020 at 11:12product_cat
– Tom J Nowell ♦ Commented Oct 5, 2020 at 11:12