2017-05-26 74 views
0

我使用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

回答

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錯誤。

+0

非常感謝配偶:) – Atif

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;