条件 is_product_category() 不适用于单个产品模板。在这种情况下,正确的条件是两个的组合:
if ( is_product() && has_term( 'categoryname', 'product_cat' ) ) {
// display my customized field
}
....
您似乎正在尝试覆盖 content-single-product.php 模板。
在你的 ELSE 语句中移动 woocommerce_single_product_summary 钩子不是一个好主意,只有当你不想显示 'categoryname' 产品 那 3 个钩子时功能:
* @hooked woocommerce_output_product_data_tabs - 10
* @hooked woocommerce_upsell_display - 15
* @hooked woocommerce_output_related_products - 20
您可以将代码 (在您的活动子主题或主题的 function.php 文件中) 嵌入到一个挂钩函数中,而不是(而不是在此处覆盖模板),使用这 2 个挂钩中更方便的一个:
//In hook 'woocommerce_single_product_summary' with priority up to 50.
add_action( 'woocommerce_single_product_summary', 'displaying_my_customized_field', 100);
function displaying_my_customized_field( $woocommerce_template_single_title, $int ) {
if ( is_product() && has_term( 'categoryname', 'product_cat' ) ) {
// echoing my customized field
}
};
或
// In hook 'woocommerce_after_single_product_summary' with priority less than 10
add_action( 'woocommerce_after_single_product_summary', 'displaying_my_customized_field', 5);
function displaying_my_customized_field( $woocommerce_template_single_title, $int ) {
if ( is_product() && has_term( 'categoryname', 'product_cat' ) ) {
// echoing my customized field
}
};