Average value of fields in wordpress posts
All my posts has a rating of 1-5 and on my category page I want to display the average rating of all the posts of that category. So I kno开发者_C百科w where the field with the value is within the post loop, but how can I get those values and calculate the average outside the post loop?
The easiest solution is probably to construct an array during the loop and average it at the end.
// Before the loop
$ratings = array();
// During the loop
$ratings[] = $post_rating;
// After the loop
$average = array_sum($ratings) / count($ratings);
精彩评论