如何为没有评论的产品显示消息而不是星级
How to display a message instead of star rating for products with no reviews
我正在使用基于 this answer 的功能将星级评分添加到产品循环(除非是首页):
add_action('woocommerce_shop_loop_item_title', 'add_star_rating' );
function add_star_rating()
{
if(!is_front_page()){
global $woocommerce, $product;
$average = $product->get_average_rating();
echo '<div class="star-rating"><span style="width:'.( ( $average / 5 ) * 100 ) . '%"><strong itemprop="ratingValue" class="rating">'.$average.'</strong> '.__( 'out of 5', 'woocommerce' ).'</span></div>';
}
}
如果还没有评论,页面会显示 5 颗灰色星星。我该如何更改它,以便在还没有产品评论时显示文本 'Be the first to review' 或类似的静态文本。
然后我还可以使用它在产品页面上很好地添加 'Be the first to review',如果没有评论,则不会显示星级。我找不到计算评论数并检查是否为零的方法。
我也试过这个,但似乎没什么区别:
add_action('woocommerce_shop_loop_item_title', 'add_star_rating' );
function add_star_rating()
{
if(!is_front_page()){
global $woocommerce, $product;
$average = $product->get_average_rating();
$count = $product->get_rating_counts();
if ($count > 0){
echo '<div class="star-rating"><span style="width:'.( ( $average / 5 ) * 100 ) . '%"><strong itemprop="ratingValue" class="rating">'.$average.'</strong> '.__( 'out of 5', 'woocommerce' ).'</span></div>';
}
else {
echo '<div>No reviews yet</div>';
}
}
}
这应该足以作为额外检查
function add_star_rating() {
// Check if reviews ratings are enabled - WooCommerce Settings
if ( ! wc_review_ratings_enabled() ) {
return;
}
if( !is_front_page() ) {
global $product;
// Get average
$average = $product->get_average_rating();
// Average > 0
if ($average > 0) {
echo '<div class="star-rating"><span style="width:'.( ( $average / 5 ) * 100 ) . '%"><strong itemprop="ratingValue" class="rating">'.$average.'</strong> '.__( 'out of 5', 'woocommerce' ).'</span></div>';
} else {
echo '<div>No reviews yet</div>';
}
}
}
add_action('woocommerce_shop_loop_item_title', 'add_star_rating', 10 );
我正在使用基于 this answer 的功能将星级评分添加到产品循环(除非是首页):
add_action('woocommerce_shop_loop_item_title', 'add_star_rating' );
function add_star_rating()
{
if(!is_front_page()){
global $woocommerce, $product;
$average = $product->get_average_rating();
echo '<div class="star-rating"><span style="width:'.( ( $average / 5 ) * 100 ) . '%"><strong itemprop="ratingValue" class="rating">'.$average.'</strong> '.__( 'out of 5', 'woocommerce' ).'</span></div>';
}
}
如果还没有评论,页面会显示 5 颗灰色星星。我该如何更改它,以便在还没有产品评论时显示文本 'Be the first to review' 或类似的静态文本。
然后我还可以使用它在产品页面上很好地添加 'Be the first to review',如果没有评论,则不会显示星级。我找不到计算评论数并检查是否为零的方法。
我也试过这个,但似乎没什么区别:
add_action('woocommerce_shop_loop_item_title', 'add_star_rating' );
function add_star_rating()
{
if(!is_front_page()){
global $woocommerce, $product;
$average = $product->get_average_rating();
$count = $product->get_rating_counts();
if ($count > 0){
echo '<div class="star-rating"><span style="width:'.( ( $average / 5 ) * 100 ) . '%"><strong itemprop="ratingValue" class="rating">'.$average.'</strong> '.__( 'out of 5', 'woocommerce' ).'</span></div>';
}
else {
echo '<div>No reviews yet</div>';
}
}
}
这应该足以作为额外检查
function add_star_rating() {
// Check if reviews ratings are enabled - WooCommerce Settings
if ( ! wc_review_ratings_enabled() ) {
return;
}
if( !is_front_page() ) {
global $product;
// Get average
$average = $product->get_average_rating();
// Average > 0
if ($average > 0) {
echo '<div class="star-rating"><span style="width:'.( ( $average / 5 ) * 100 ) . '%"><strong itemprop="ratingValue" class="rating">'.$average.'</strong> '.__( 'out of 5', 'woocommerce' ).'</span></div>';
} else {
echo '<div>No reviews yet</div>';
}
}
}
add_action('woocommerce_shop_loop_item_title', 'add_star_rating', 10 );