我正在使用ACF,如果custom_field号码
,我不知道如何设法替换div类名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
答案 0 :(得分:1)
我假设你想获得赌场评级并根据出现的内容显示不同的课程?
在这种情况下,您可以使用此代码:
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错误。
答案 1 :(得分:0)
稍微更紧凑的版本应该达到同样的目的
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;