循环显示星级

时间:2014-10-29 01:56:38

标签: php css html5 wordpress woocommerce

我正在试图弄清楚如何将产品页面上显示的星级评分输出到循环中。这是我试图将星形输出到循环

的代码
<div class="product-rating">
    <?php echo '<div class="star-rating" title="'.sprintf(__( 'Rated %s out of 5', 'woocommerce' ), $average).'"><span style="width:'.( ( $average / 5 ) * 100 ) . '%"><strong itemprop="ratingValue" class="rating">'.$average.'</strong> '.__( 'out of 5', 'woocommerce' ).'</span></div>'; ?>
</div>

此时产品仅显示文字,但未输出正确的评级。我已经给它一个测试评级,它只显示“满分5分”这里是帖子的截图

http://i255.photobucket.com/albums/hh140/testament1234/product_zpsaf2b219a.jpg

<ul class="products eight columns">
<?php
    $args = array( 'post_type' => 'product', 'posts_per_page' => 10, 'product_cat' => 'Lumiere', 'orderby' => 'rand' );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>

            <li class="product product-items ">    

                <div class="product-item">

                    <?php woocommerce_show_product_sale_flash( $post, $product ); ?>

                    <div class="product-thumbnail">
                    <?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" />'; ?>
                    </div>

                    <div class="product-info">
                    <h3><a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>"><?php the_title(); ?></a></h3>

                    <?php echo $product->get_sku(); ?>
                    <?php echo apply_filters( 'woocommerce_short_description', $post->post_excerpt ) ?>


                    <?php if ( $price_html = $product->get_price_html() ) : ?>
                        <span class="price"><?php echo $price_html; ?></span>
                    <?php endif; ?>  


                    <div class="product-rating">
                    <?php echo '<div class="star-rating" title="'.sprintf(__( 'Rated %s out of 5', 'woocommerce' ), $average).'"><span style="width:'.( ( $average / 5 ) * 100 ) . '%"><strong itemprop="ratingValue" class="rating">'.$average.'</strong> '.__( 'out of 5', 'woocommerce' ).'</span></div>'; ?>
                    </div>

                    <?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>

                    </div>

                </div>



            </li>

<?php endwhile; ?>
<?php wp_reset_query(); ?>
</ul><!--/.products-->

1 个答案:

答案 0 :(得分:0)

如果我是对的,解决方案将如下所示:

<div class="product-rating">
    <?php if ($average = $product->get_average_rating()) : ?>
        <?php echo '<div class="star-rating" title="'.sprintf(__( 'Rated %s out of 5', 'woocommerce' ), $average).'"><span style="width:'.( ( $average / 5 ) * 100 ) . '%"><strong itemprop="ratingValue" class="rating">'.$average.'</strong> '.__( 'out of 5', 'woocommerce' ).'</span></div>'; ?>
    <?php endif; ?>
</div>