我正在尝试为WooCommerce中的不同类别显示不同的自定义字段。
我在content-single-product.php模板文件中使用了以下条件语句:
if(is_product_category('categoryname'))
{
// display my customized field
}
else
{
do_action( 'woocommerce_after_single_product_summary' );
}
但这对我不起作用。
有没有更好的方法来纠正此问题?
谢谢。
条件is_product_category()在单个产品模板中对您不起作用。在这种情况下,正确的条件是两个条件的组合:
if ( is_product() && has_term( 'categoryname', 'product_cat' ) ) {
// display my customized field
}
....
看来您正在尝试覆盖content-single-product.php
模板。
woocommerce_single_product_summary
仅当您不想为'categoryname'
产品显示具有3个挂钩函数的产品时,才将ELSE语句内的钩子移动不是一个好主意:
* @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
}
};
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句