我的functions.php 中有一个计算平均用户评分的函数。它适用于自定义帖子类型并且效果很好。问题是它只适用于一种类型,我确信必须有一种更简洁的方法,然后必须重复该功能三次。问题似乎是这条线
<code>
$child_posts = types_child_posts('business-reviews', 'restaurant-cafe-rev', 'travel-tip-review');
</code>
这是我的完整代码 - 如果有人可以指导我。谢谢
<code>
// Adds Calculation of average and shortcode for ratings
add_shortcode('rating-average', 'rating_average_func');
function rating_average_func()
{
$child_posts = types_child_posts('business-reviews', 'restaurant-cafe-rev', 'travel- tip-review');
$sum = 0;
$num = 0;
foreach ($child_posts as $child_post) {
if(isset($child_post->fields['ratings']))
{
$sum += $child_post->fields['ratings'];
$num ++;
}
}
$average = 0;
if($num>0)
{
$average = $sum/$num;
}
$res = $average;
if($average==0) $res = 0;
if($average>0.001 && $average<0.5)$res = 0.5;
if($average>0.501 && $average<1) $res = 1;
if($average>1.001 && $average<1.5) $res = 1.5;
if($average>1.501 && $average<2) $res = 2;
if($average>2.001 && $average<2.5) $res = 2.5;
if($average>2.501 && $average<3) $res = 3;
if($average>3.001 && $average<3.5) $res = 3.5;
if($average>3.501 && $average<4) $res = 4;
if($average>4.001 && $average<4.5) $res = 4.5;
if($average>4.501 && $average<5) $res = 5;
//... here put more condition ...
return $res;
}
//Add shortcode for Reviews Count
add_shortcode('reviews_total', 'reviews_total_func');
function reviews_total_func()
{
$child_posts = types_child_posts('business-reviews', 'restaurant-cafe-rev', 'travel-tip-review');
return count($child_posts);
}
</code>