2 回答
TA贡献1777条经验 获得超3个赞
要在此特定类别的产品页面中显示文本,您应该添加条件标签is_product()并使用如下功能检查它是否具有正确的类别has_term():
add_action( 'woocommerce_after_main_content', 'add_my_text' );
function add_my_text() {
//1st grab the product/post object for the current page if you don't already have it:
global $post;
//2nd you can get the product category term objects (the categories) for the product
$terms = wp_get_post_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ) $categories[] = $term->slug;
//Then we just have to check whether a category is in the list:
if ( is_product_category( 'category1' ) || is_product() && in_array( 'category1', $categories ) ) {
echo '<p>This is my extra text.</p>';
}
}
对于这个if (function_exists(...问题,这是一个向后兼容性的问题,正如这个答案中提到的:https ://wordpress.stackexchange.com/a/111318/136456
TA贡献1802条经验 获得超6个赞
这有效:
add_action( 'woocommerce_after_single_product_summary', 'add_text' );
function add_text() {
if ( has_term( 'nail', 'product_cat' ) ) {
echo 'Something';
} elseif ( has_term( 'tables', 'product_cat' ) ) {
echo 'Something else';
}
}
- 2 回答
- 0 关注
- 143 浏览
添加回答
举报
