如何根据号码范围
问题描述:
我使用ACF和不知道如何管理替换DIV类名,如果custom_field数量是如何根据号码范围
equal and less then 30 class="color1"
equal and less then 50 class="color2"
equal and less then 90 class="color3"
Can you please tell me how to do that?
if($post_objects):
foreach($post_objects as $post_object):
echo the_field("casino_rating", $post_object->ID);
endforeach; endif;
感谢 与Atif
答
我假设改变股利类你想获得赌场评级,并根据显示的内容显示不同的课程?
在这种情况下,你可以使用此代码:
if($post_objects):
foreach($post_objects as $post_object):
$casino_rating = get_field("casino_rating", $post_object->ID);
// This part here will decide what class to get
if($casino_rating < 30){
echo 'class="color1"';
} elseif($casino_rating < 50){
echo 'class="color2"';
} elseif($casino_rating < 90){
echo 'class="color3"';
}
endforeach;
endif;
你可能要小心,虽然并确保这仅仅是输出class=
如果有附属的元素任何其他类别的,否则就会有HTML错误。
答
稍微更紧凑的版本,要达到同样的事情
if($post_objects):
foreach($post_objects as $post_object):
$rating = get_field("casino_rating", $post_object->ID);
$color = ($rating >= 31) ? "color2" : "color1";
$color = ($rating >= 51) ? "color3" : $color;
echo 'class="'.$color.'"';
endforeach;
endif;
,或者如果你要使用这个有很多做一个功能它在你的页面的底部
function color($rating) {
$color = ($rating >= 31) ? "color2" : "color1";
$color = ($rating >= 51) ? "color3" : $color;
return $color;
}
然后你可以做
if($post_objects):
foreach($post_objects as $post_object):
$rating = get_field("casino_rating", $post_object->ID);
echo 'class="'.color($rating).'"';
endforeach;
endif;
非常感谢配偶:) – Atif