Total number of reviews for current product [count_reviews] рус
Fri May 23 2025 11:48:34 GMT+0000 (Coordinated Universal Time)
Saved by
@mastaklance
// Total number of reviews for current product [count_reviews
add_shortcode('count_reviews', function() {
$product_id = get_queried_object_id();
if ( ! $product_id ) {
return '';
}
$args = array(
'post_type' => 'simple-reviews',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'review_post_id',
'value' => $product_id,
'compare' => '=',
),
array(
'key' => 'rating_review',
'value' => '',
'compare' => '!=',
),
),
'fields' => 'ids',
);
$query = new WP_Query($args);
$total_reviews = count($query->posts);
$label = plural_form($total_reviews, 'отзыв', 'отзыва', 'отзывов');
return "{$total_reviews} {$label}";
});
// Функция склонения слов по числам
function plural_form($number, $form1, $form2, $form5) {
$n = abs($number) % 100;
$n1 = $n % 10;
if ($n > 10 && $n < 20) return $form5;
if ($n1 > 1 && $n1 < 5) return $form2;
if ($n1 == 1) return $form1;
return $form5;
}
content_copyCOPY
Comments