我正在尝试添加以下内容:“4.5/5(5 条评论)”,但没有将“”添加到所有 WooCommerce 产品的评论选项卡中。
我已经完成添加星星和一些文字,但我不知道如何将文字变成“4.5/5(5条评论)”。这是我正在使用的代码。需要明确的是,我想保留星星。
add_filter( 'woocommerce_product_reviews_tab_title', 'add_stars_to_review_tab', 98 );
function add_stars_to_review_tab( $title ) {
global $product;
$average_rating = $product->get_average_rating();
$rating_count = $product->get_rating_count();
$review_count = $product->get_review_count();
if( ! empty($average_rating) && $average_rating > 0 )
$title = '<div>' . $title . '</div>
<div class="stars">' . wc_get_rating_html($average_rating) . ' based on ' . $rating_count. ' reviews' . '</div>';
return $title;
}
最佳答案
更新 2:以下代码将在 Woocommerce 单一产品页面选项卡中的评论项目菜单标题和星星下方显示类似 “4.5/5(5 条评论)” 的内容:
add_filter( 'woocommerce_product_reviews_tab_title', 'add_stars_to_review_tab', 98 );
function add_stars_to_review_tab( $title ) {
global $product;
$average_rating = $product->get_average_rating();
$rating_count = $product->get_rating_count();
$review_count = $product->get_review_count();
if( ! empty($average_rating) && $average_rating > 0 ) {
return '<div>' . $title . '</div>' . wc_get_rating_html($average_rating) . sprintf(
'<div class="stars">%s / 5 (%s %s)</div>',
$average_rating,
$review_count,
_n( "review", "reviews", $review_count, "woocommerce" )
);
}
return $title;
}
代码位于事件子主题(事件主题)的 function.php 文件中。经过测试并有效。
关于php - 将星级和平均值添加到 Woocommerce 中的产品 "Review"项目菜单中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53344184/