我正在尝试添加平均星级,并在其旁边添加一个计数器,以显示还剩多少条评论。我将其仅用于我的商店页面,并通过将以下代码添加到主题(醒目的)functions.php文件中来实现了这一点:
add_action( 'woocommerce_after_shop_loop_item_title','gkartikey_woocommerce_template_loop_rating',5 );
if ( ! function_exists( 'gkartikey_woocommerce_template_loop_rating' ) ) {
/**
* Display the average rating in the loop.
*/
function gkartikey_woocommerce_template_loop_rating() {
wc_get_template( 'loop/rating.php' );
}
}
add_filter( 'woocommerce_product_get_rating_html',function ( $html,$rating,$count ) {
global $product;
if ( $html && is_archive() && $product) {
$html .= sprintf( '<span>%s</span>',$product->get_rating_count() );
}
return $html;
},10,3 );
问题是计数器没有显示在其他页面上,例如我使用Elementor添加产品的主页上。