在循环中显示星级评分

5

我正在尝试找出如何将产品页面显示的星级评分输出到循环中。以下是我尝试将星级输出到循环的代码:

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

不了解周围代码的情况下,我猜测您的循环中没有设置$average。在开放div标签后添加以下内容:<?php var_dump($average); ?>。 - joehart
在添加了您指定的代码后,它显示出“NULL”文本。我还添加了整个代码供您查看。 - clestcruz
我的猜测是,$product对象中有一个属性需要获取以填充$average,类似于您之前几行设置$price_html = $product->get_price_html()的方式。如果您var_dump($product),您是否看到其中有一个平均评分的值? - joehart
1个回答

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>

你的解决方案有效。谢谢,伙计。现在我的唯一问题是尝试实现字体图标,以便显示星星而不是文本。 - clestcruz
帮不了你,但很高兴你的PHP问题已经解决了。 - joehart

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接