试试这个,它对我有用。将此代码放在主题函数中。php文件。
function dynamic_product_category_widget($title, $widet_instance, $widget_id) {
if ( $widget_id !== \'woocommerce_product_categories\' )
return $title;
if ( is_product_category() ) {
$cat_title = single_cat_title(\'<p class="widget-title footer-widget">\', \'</p>\');
return $cat_title;
} elseif ( is_product() ) {
$cat = get_the_terms( $product->ID, \'product_cat\' );
foreach ($cat as $categoria) {
if($categoria->parent == 0){
$topCategory = $categoria;
}
}
return \'<p class="widget-title footer-widget">\'.$topCategory->name.\'</p>\';
}
return $title;
}
add_filter ( \'widget_title\' , \'dynamic_product_category_widget\', 10, 3);