如果函数返回0到6之间的数字,则更改背景颜色

时间:2016-05-02 13:03:52

标签: php html css

我在网站上显示了PHP功能:

<div class="metacontent"><div><span> <?php the_field('metascore'); ?> </span></div> </div>

这将返回电影的元记录,由wordpress中的插件定义,作者可以在编辑帖子时输入分数。

我现在想用不同的背景颜色设置结果的样式,具体取决于电影获得的分数:

.metacontentG {
    position: relative;
    width: 52px;
    height: 52px;
    background-color: #6c3;
}

.metacontentY {
    position: relative;
    width: 52px;
    height: 52px;
    background-color: #fc3;
}

.metacontentR {
    position: relative;
    width: 52px;
    height: 52px;
    background-color: #f00;
}

换句话说,如果分数高于6,我想使用.metacontentG(函数the_field(&#39; metascore&#39;)返回的值大于6)。如果分数在4到6之间,我想使用.metacontentY。如果分数低于4,我想使用.metacontentR。

我将如何解决这个问题?

4 个答案:

答案 0 :(得分:0)

更新到您的函数the_field以返回array(score => 6, class =>metacontentR)或类似内容,然后在您的html中,您可以将字符串用作类

<div class="<?php the_field('metascore')['class'] ?>"><div><span> <?php the_field('metascore')['score']; ?> </span></div> </div>

答案 1 :(得分:0)

首先制作一个if语句来确定它属于哪个类别:

$score = get_field('metascore'); 

if($score < 4) {    
  $class = "R";
} elseif($score < 6) {
  $class = "Y";
} else {
  $class = "G";
}

然后像这样回应:

<div class="metacontent<?php echo $class; ?>"><div><span> <?php echo $score; ?> </span></div> </div>

答案 2 :(得分:0)

您可以使用: -

<?php
$response = the_field('metascore')
?>
<div class="<?php if($response==1) { echo "metacontentG"; } if($response==2) { echo "metacontentC"; } ?>"><div><span> <?php the_field('metascore'); ?> </span></div> 

您也可以尝试使用开关案例

答案 3 :(得分:0)

$mscore = get_field('metascore'); 
$className = "G";
if($mscore !="")
{
    if($mscore > 4 && $mscore < 6) {    
        $className = "Y";
    } elseif($mscore > 6) {
        $className = "G";
    } elseif($mscore < 4) {
        $className= "R";
    }
}

<div class="<?php print $className;?>" ><div><span> <?php the_field('metascore'); ?> </span></div> </div>