3 回答

TA貢獻1821條經驗 獲得超6個贊
無需將其硬編碼到模板中。使用 CSS 通過稍微復雜的選擇器列表來實現此目的。您可以將其包裝在媒體查詢中,因為我看到您有.假設你正在使用引導,那將是你會使用的。col-md-3@include media-breakpoint-up(md)
.container {
display: flex;
flex-wrap: wrap;
background: gray;
}
.item {
display: flex;
justify-content: center;
align-items: center;
width: 25%;
height: 100px;
background: white;
}
.item:nth-child(4n+2):not(:nth-child(8n-2)), /* 2, 10, 18… */
.item:nth-child(4n+4):not(:nth-child(8n)), /* 4, 12, 20… */
.item:nth-child(4n+1):not(:nth-child(8n+1)), /* 5, 13, 21… */
.item:nth-child(4n+3):not(:nth-child(8n+3)) { /* 7, 15, 23… */
background: red;
}
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
<div class="item">7</div>
<div class="item">8</div>
<div class="item">9</div>
<div class="item">10</div>
<div class="item">11</div>
<div class="item">12</div>
<div class="item">13</div>
<div class="item">14</div>
<div class="item">15</div>
<div class="item">16</div>
<div class="item">17</div>
<div class="item">18</div>
<div class="item">19</div>
<div class="item">20</div>
</div>
現在來解釋一下選擇器邏輯:您希望將每個第 2 個和第 4 個子項定位到奇數行,將第 1 個和第 3 個子項定位到偶數行上。為此,您可以分別定位:
每 2 個項目不是 6 的倍數
每 4 個項目不是 8 的倍數
每 5 個項目不是 9 的倍數
每 7 個項目不是 11 的倍數
希望這有幫助!

TA貢獻1765條經驗 獲得超5個贊
好吧,這有點棘手,因為您正在更改4塊的決策順序。要確定這一點,您可以有一個初始設置為 的額外變量。$flagfalse
因此,檢查結果為:
if ($flag && ($c & 1) == 0 || !$flag && ($c & 1) == 1)
這表示如果我們處于奇數迭代(當$flag為假時)并且當前是奇數,或者我們處于偶數迭代(當$flag為真時)并且電流是偶數。$c$c
當重新初始化以指示下一行翻轉時,我們會重置該標志。$c0
片段:
<?php
$c = 0;
$product = wc_get_product( $product_id );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="col-md-3 pl-0 pr-0">
<a href="<?php the_permalink(); ?>">
<div class="product-list-item <?php if ($flag && ($c & 1) == 0 || !$flag && ($c & 1) == 1) { ?>bg-grey<?php } ?>">
<div class="img-wrapper">
<?php echo $product->get_image('full', array('class' => 'img-fluid')); ?>
</div>
<div class="product-content">
<span class="product-title"><?php echo $product->get_name(); ?></span>
<span class="product-price">€ <?php $price = $product->price; $price_incl_tax = $price + round($price * ( 0 / 100 ), 2); $price_incl_tax = number_format($price_incl_tax, 2, ",", "."); $price = number_format($price, 2, ",", "."); echo $price_incl_tax; ?> incl. BTW</span>
</div>
</div>
</a>
</div>
<?php $c = ($c + 1) % 4; $flag = $c == 0 ? !$flag : $flag; endwhile; wp_reset_query(); // Remember to reset ?>

TA貢獻1155條經驗 獲得超0個贊
如果你想在php中完成它,我發現工作方法是使用兩個與透明和灰色不同步的數組。
然后從一行上的一個數組回顯,然后切換。
這不是一個好的代碼,但它可以解決問題。
$arr1 = ['transparent', 'grey', 'transparent', 'grey'];
$arr2 = ['grey', 'transparent', 'grey', 'transparent'];
$i = 0;
$bool = true;
Your loop{
if($bool){
echo 'color ' . $arr1[$i] . "\n";
}else{
echo 'color ' . $arr2[$i] . "\n";
}
$i = ++$i % 4;
if($i ==0) $bool = !$bool;
}
下面是一個正在運行的代碼示例:
https://3v4l.org/XdcHB
- 3 回答
- 0 關注
- 104 瀏覽
添加回答
舉報