最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

shortcode - How to test If a post has a particular term

programmeradmin3浏览0评论

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
  • so you're asking if a post ( of type 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:02
  • I've cleaned up your code and fixed an unrelated bug ( is is_single was false then null 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:07
  • no not that, am talking about a product (Woocommerce), if the product is in the category XX a specific code should display in the short description. – Ahmad Darwish Commented Oct 5, 2020 at 11:10
  • Yes, products are posts of type product, much like pages are posts of type page, 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:12
  • even the WooCommerce official docs say so docs.woocommerce/document/installed-taxonomies-post-types, product categories are just a custom taxonomy with the internal name product_cat – Tom J Nowell Commented Oct 5, 2020 at 11:12
Add a comment  | 

1 Answer 1

Reset to default 0

You 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" )

发布评论

评论列表(0)

  1. 暂无评论