条件 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 文件中)嵌入到钩子函数中:
//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
}
};