2 回答

TA貢獻1777條經(jīng)驗 獲得超3個贊
要在此特定類別的產(chǎn)品頁面中顯示文本,您應(yīng)該添加條件標簽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條經(jīng)驗 獲得超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 關(guān)注
- 108 瀏覽
添加回答
舉報