0

我对 WordPress 很陌生,并且使用 wordpress 开发了一个在线目录。

我需要在搜索页面上显示产品评级。使用下面的代码,我可以显示星星,但填充的和空的星星没有出现

<?php 
$average = $parent_product->get_average_rating();
$rating_args = array('rating' => $average, 'type' => 'rating', 'number' => 1234,);
$defaults = array('rating' => 0, 'type' => 'rating', 'number' => 0, 'echo' => true,);
$r = wp_parse_args($args, $defaults);

// Non-English decimal places when the $rating is coming from a string
$rating = (float)str_replace(',', '.', $r['rating']);

// Convert Percentage to star rating, 0..5 in .5 increments
if('percent' === $r['type']) {
    $rating = round($rating / 10, 0) / 2;
}

// Calculate the number of each type of star needed
$full_stars = floor($rating);
$half_stars = ceil($rating - $full_stars);
$empty_stars = 5 - $full_stars - $half_stars;
if($r['number']) {
    /* translators: 1: The rating, 2: The number of ratings */
    $format = _n('%1$s rating based on %2$s rating', '%1$s rating based on %2$s ratings', $r['number']);
    $title = sprintf($format, number_format_i18n($rating, 1), number_format_i18n($r['number']));
}
else {
    /* translators: 1: The rating */
    $title = sprintf(__('%s rating'), number_format_i18n($rating, 1));
}

$output = '<div class="star-rating">';
$output .= '<span class="screen-reader-text">'.$title.'</span>';
$output .= str_repeat('<div class="star star-full" aria-hidden="true"></div>', $full_stars);
$output .= str_repeat('<div class="star star-half" aria-hidden="true"></div>', $half_stars);
$output .= str_repeat('<div class="star star-empty" aria-hidden="true"></div>', $empty_stars);
$output .= '</div>';

以下是我在 style.css 中放入的 CSS

@font-face {
    font-family: "dashicons";
    src: url("../fonts/dashicons.eot");
}

@font-face {
    font-family: "dashicons";
    src: url(data:application/x-font-woff;charset=utf-8;base64,/* !! Large amount of data removed, see wp-includes/css/dashicons.css for complete data !! */) format("woff"),
        url("../fonts/dashicons.ttf") format("truetype"),
        url("../fonts/dashicons.svg#dashicons") format("svg");
    font-weight: normal;
    font-style: normal;
}

.star-rating .star-full:before {
    content: "\f155";
}

.star-rating .star-half:before {
    content: "\f459";
}

.star-rating .star-empty:before {
    content: "\f154";
}

.star-rating .star {
    color: #0074A2;
    display: inline-block;
    font-family: dashicons;
    font-size: 20px;
    font-style: normal;
    font-weight: 400;
    height: 20px;
    line-height: 1;
    text-align: center;
    text-decoration: inherit;
    vertical-align: top;
    width: 20px;
}

以下是星星在网页中的显示方式: 在此处输入图像描述

在此处输入图像描述

4

0 回答 0