我的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>
答案 0 :(得分:0)
自从我使用该插件以来已经有一段时间......
你试过这个吗?
$child_posts = types_child_posts(array('business-reviews', 'restaurant-cafe-rev', 'travel-tip-review'));