我需要计算评论的平均评分,并将该值作为评级星放在其他页面上。例如。如果平均评级值是“3星”。我想将该值打印为“3星图像”,并将该图像放在另一页上。我在很多网站上问过这个问题,但没有得到任何答案。请帮帮我一个人
这是网址
http://www.smashingmagazine.com/2012/05/08/adding-custom-fields-in-wordpress-comment-form/
$plugin_url_path = WP_PLUGIN_URL;
if( $commenttitle = get_comment_meta( get_comment_ID(), 'title', true ) ) {
$commenttitle = '<strong>' . esc_attr( $commenttitle ) . '</strong><br/>';
$text = $commenttitle . $text;
}
if( $commentrating = get_comment_meta( get_comment_ID(), 'rating', true ) ) {
$commentrating = '<p class="comment-rating"> <img src="'. $plugin_url_path .
'/ExtendComment/images/'. $commentrating . 'star.gif"/><br/>Rating: <strong>'. $commentrating .' / 5</strong></p>';
$text = $text . $commentrating;
return $text;
} else {
return $text;
}
}
答案 0 :(得分:1)
我找到了一个解决方案,给出了平均评分。请在extendcomment.php中添加以下函数
function average_rating() {
global $wpdb;
$post_id = get_the_ID();
$ratings = $wpdb->get_results("
SELECT $wpdb->commentmeta.meta_value
FROM $wpdb->commentmeta
INNER JOIN $wpdb->comments on $wpdb->comments.comment_id=$wpdb->commentmeta.comment_id
WHERE $wpdb->commentmeta.meta_key='rating'
AND $wpdb->comments.comment_post_id=$post_id
AND $wpdb->comments.comment_approved =1
");
$counter = 0;
$average_rating = 0;
if ($ratings) {
foreach ($ratings as $rating) {
$average_rating = $average_rating + $rating->meta_value;
$counter++;
}
//round the average to the nearast 1/2 point
return (round(($average_rating/$counter)*2,0)/2);
} else {
//no ratings
return 'no rating';
}
}
用&#34;&#34;调用该功能这将给出数值平均值。要将平均值显示为图像,请参阅&#34;函数modify_comment($ text)&#34;。