我需要计算评论的平均评分,并将该值作为评分星放在其他页面。为例。如果平均评级值为"3星"。我想打印该值为"3星图像",并将该图像放在另一个页面。我在很多网站上问过这个问题,但没有得到任何答案。谁来帮帮我
这是URL
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;
}
}
我找到了一个解决方案,它给出了平均评级。请在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';
}
}
调用带有"的函数,该函数将给出数值平均值。要将平均值显示为图像,请参阅"function modify_comment($text)"。