dqfaom241360 2017-11-08 09:14
浏览 20
已采纳

计算逻辑输出不正确的Wordpress获取字段

I have created a reviews widget that collects all the reviews on the website and then creates an aggregate rating via a calculation. It seemed to work perfectly fine with around 7 reviews and below however I recently added more to make it 13 and the calculation seems to now be incorrect and I am not sure why can anyone help?

EDIT: Updated code to be outside of loop, did not resolve the issue.

<?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('review_score'); is input as an integer for the review score.

EDIT: Displaying the code that displays the widget

<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>

Below is an image to explain the width variable, the golden star BG is what is altered by the $width variable in order to give an accurate graphic of aggregate rating.

Review Width

  • 写回答

1条回答 默认 最新

  • donglu9978 2017-11-08 09:37
    关注

    Move your calculation after the the loop, get only published reviews,change the posts_per_page param to -1 to get all the review posts

    <?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(); ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题