gpt4 book ai didi

php - 如果 PHP 中的条件动态更改 css

转载 作者:行者123 更新时间:2023-11-28 12:43:54 25 4
gpt4 key购买 nike

我正在使用 wordpress 主题,当某个变量的值为偶数或奇数时,我确实想使用 php 更改 2 个 block 的 css..

这是我的代码:

<?php 
$i = 0;
while ( have_posts() ) {

// Override
if(($i % 2) == 1) { ?>

<style type="text/css">
.grid-box-img {
width: 54%;
float: left;
}

.entry-head {
width: 56%;
background-color: #fff;
position: absolute;
z-index: 10;
margin-left: 43%;
margin-top: 27%;
}
</style>
<?php
}
else { ?>
<style type="text/css">
.grid-box-img {
width: 54%;
float: right;
}

.entry-head {
width: 56%;
background-color: #fff;
position: absolute;
z-index: 10;
margin-top: 27%;
}
</style>
<?php
}
$i++;
the_post();
} ?>

但是,不幸的是它不起作用...它只应用最后一个值的 CSS。

我该怎么做?谢谢:)

最佳答案

最佳做法是将两个类组成一个用于偶数,另一个用于奇数,并在需要的地方放置一个 if 条件。

<a href="/" class="<?php if(($i % 2) == 1){ echo 'even';} else { echo 'odd';} ?> ">Link </a>

在你的 CSS 中:

.even{
//your styling for even class
}
.odd{
//your styling for odd class
}

关于php - 如果 PHP 中的条件动态更改 css,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17512738/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com