Trong WordPress có tích hợp sẵn hàm để hiển thị sao đánh giá cho plugin hoặc theme hoặc một đối tượng nào đấy. Dùng đoạn code bên dưới để hiển thị sao đánh giá bên ngoài admin.
if ( ! function_exists( 'wp_star_rating' ) ) {
require_once( ABSPATH . 'wp-admin/includes/template.php' );
}
wp_star_rating( array( 'rating' => $average ) );
Điền giá trị trung bình vào hàm để hiển thị số lượng ngôi sao đánh giá theo khung 5 sao. Tuy nhiên, để hàm này hoạt động bạn cần load style dashicons và thêm 1 vài css.
wp_enqueue_style( 'dashicons' );
.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: #e7721b;
display: inline-block;
font-family: dashicons, serif;
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;
}