我创建了一个评论小部件,用于收集网站上的所有评论,然后通过计算创建总评分。它似乎与大约7个评论及以下工作完全正常但是我最近添加了更多使其成为13并且计算现在似乎不正确并且我不确定为什么有人可以帮忙?
编辑:更新的代码不在循环之内,无法解决问题。
<?php
$post_args = array(
'post_type' => 'review'
);
$title = apply_filters( 'widget_title', $instance['title'] );
$code = apply_filters( 'widget_code', $instance['code'] );
$desc = apply_filters( 'widget_desc', $instance['desc'] );
$link = apply_filters( 'widget_link', $instance['link'] );
$total_rating = 0; //Sets the total rating to 0
$post_list = new wp_query( $post_args ); //collects reviews
if( $post_list->have_posts() ) : while( $post_list->have_posts() ) :
$post_list->the_post(); //initiates reviews posts
$all_ratings = get_field('review_score'); //collects all integers
$total_rating += $all_ratings; //gets all integers and adds them together
?>
<?php endwhile; endif;
$multiply = $total_rating * 20; //Multiply total rating by 20 for width
$total_reviews = wp_count_posts( 'review' )->publish; //Counts published reviews
$width = $multiply / $total_reviews; //Divides multiply by amount of review for accurate % width of div.
$divide = number_format($total_rating / $total_reviews, 1); //Divides the total of all reviews by amount of reviews e.g. 2 Review both 5/5 = 10/2=5=Correct.
wp_reset_query(); ?>
<?php endwhile; endif; wp_reset_query(); ?>
FYI - get_field(&#39; review_score&#39;);输入为评论分数的整数。
编辑:显示显示小部件的代码
<aside class="widget widget_reviews">
</span>
<h3><?php echo $title; ?></h3>
<?php if($width > 0) : ?>
<div itemscope itemtype="https://schema.org/Product">
<meta itemprop="name" content="Regency Chauffeurs">
<div itemprop="aggregateRating" itemscope itemtype="https://schema.org/AggregateRating">
<meta itemprop="worstRating" content="1">
<div class="average-reviews-base">
<div class="average-reviews" style="width:<?php echo $width; ?>%"></div>
</div>
<span class="average-score-link"><span itemprop="ratingValue"><?php echo $divide; ?></span> / <span itemprop="bestRating">5</span> (From <a href="<?php echo $link; ?>" title="<?php echo $title; ?>"><span itemprop="reviewCount"><?php echo $total_reviews; ?></span> <?php if($total_reviews > 1) : ?>Reviews<?php else : ?>Review<?php endif;?></a>)</span>
</div>
<div class="review-desc">
<p><?php echo $desc; ?></p>
<p><a href="<?php echo $link; ?>" title="<?php echo $title; ?>" class="button"><?php echo $code; ?></a></p>
</div>
<?php else : ?>
<div class="review-desc">
<p>There is currently no reviews, if you would like to leave a review then please <a class="underline" href="<?php echo $link; ?>" title="review">click here</a>.</p>
<p><a href="<?php echo $link; ?>" title="<?php echo $title; ?>" class="button">Leave A Review</a></p>
</div>
</div>
<?php endif;?>
</aside>
下面是一个解释宽度变量的图像,黄金星BG是由$ width变量改变的,以便给出总体评级的准确图形。
答案 0 :(得分:1)
在循环后移动您的计算,仅获取已发布的评论,将posts_per_page
参数更改为-1以获得所有评论帖
<?php
$post_args = array(
'post_type' => 'review',
'post_status'=>'publish',
'posts_per_page'=> -1
);
$total_rating = 0; //Sets the total rating to 0
$post_list = new wp_query( $post_args ); //collects reviews
if( $post_list->have_posts() ) : while( $post_list->have_posts() ) :
$post_list->the_post(); //initiates reviews posts
$all_ratings = get_field('review_score'); //collects all integers
$total_rating += $all_ratings; //gets all integers and adds them together
?>
<?php endwhile; endif;
$multiply = $total_rating * 20; //Multiply total rating by 20 for width
$total_reviews = wp_count_posts( 'review' )->publish; //Counts published reviews
$width = $multiply / $total_reviews; //Divides multiply by amount of review for accurate % width of div.
$divide = number_format($total_rating / $total_reviews, 1); //Divides the total of all reviews by amount of reviews e.g. 2 Review both 5/5 = 10/2=5=Correct.
wp_reset_query(); ?>