如何根据数字范围更改Div class
How to change Div class based on number range
我正在使用 ACF,如果 custom_field 号码是
,我不知道如何设法替换 div class 名称
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;
谢谢
阿提夫
我假设您想获得赌场评级并根据出现的内容显示不同的 class?
在这种情况下,您可以使用此代码:
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 附加到该元素时才输出 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;
我正在使用 ACF,如果 custom_field 号码是
,我不知道如何设法替换 div class 名称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;
谢谢 阿提夫
我假设您想获得赌场评级并根据出现的内容显示不同的 class?
在这种情况下,您可以使用此代码:
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 附加到该元素时才输出 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;