我正在寻找一种方法,根据我查看的类别更改Woocommerce产品类别小部件的小部件标题。
类别1------>类别1小部件的显示标题
- Sub Category
- Sub Category
- Sub Category
类别2------>类别1小部件的显示标题
- Sub Category
- Sub Category
- Sub Category
我正在处理这个问题,但似乎无法以小部件标题为目标并正确创建if-else语句。
function retitle_woo_category_widget() {
// If \'Category\' 1 is being viewed...
if ( is_product() && has_term( \'Category 1\', \'product_cat\' ) ) {
//... Remove the images
return __(\'Title of Widget for Category 1\');
}
else {
// If \'Category\' 2 is being viewed...
if ( is_product() && has_term( \'Category 2\', \'product_cat\' ) ) {
return __(\'Title of Widget for Category 2\');
}
add_filter ( \'widget_title\' , \'retitle_woo_category_widget\');
有什么建议吗?
SO网友:Sumit
您的代码有语法错误if
和else
块未关闭此外,您正在更改所有小部件标题。通过检查widget ID
只有在查看属于上述类别的单个产品页面时,当前代码才会生效。对于所有其他页面返回title
因此,标题不会为空示例:-
function retitle_woo_category_widget($title, $widet_instance, $widget_id) {
if ( $widget_id !== \'woocommerce_product_categories\' )
return $title;
// If \'Category\' 1 is being viewed...
if ( is_product() && has_term( \'Category 1\', \'product_cat\' ) ) {
//... Remove the images
return __(\'Title of Widget for Category 1\');
// If \'Category\' 2 is being viewed...
} else if ( is_product() && has_term( \'Category 2\', \'product_cat\' ) ) {
return __(\'Title of Widget for Category 2\');
}
return $title;
}
add_filter ( \'widget_title\' , \'retitle_woo_category_widget\', 10, 3);