此代码适用于类别,但不适用于子类别。
我怎样才能使它成为url(slug)中不存在的子类别?
global $post;
$terms = wp_get_post_terms( $post->ID, \'product_cat\' );
foreach ( $terms as $term ) $categories[] = $term->slug;
if ( in_array( \'lucha\', $categories ) ) {
echo \'<div class="nota-categorias-especiales">Important: this product has special conditions.
<button class="single_add_to_cart_button button alt" type="submit">Contact Us</button>
</div> \';
} else {}
?>
最合适的回答,由SO网友:Pieter Goosen 整理而成
你可以利用has_term( $term, $taxonomy, $post )
这将检查特定职位是否属于给定的术语
您可以执行类似的操作(编辑:如果在函数中使用,则添加帖子ID)
global $post;
if(has_term( \'lucha\', \'product_cat\', $post->ID )) {
//do something if post has lucha term
}else{
//do something else if the post don\'t have lucha term
}