亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

4次后復位循環

4次后復位循環

PHP
慕桂英3389331 2022-09-12 10:58:52
我正在我的頁面上循環使用產品,背景顏色如下:“透明” - “灰色” - “透明” - “灰色”每行有4種產品,但當第二行開始時,它正在做相同的順序,例如:情況1 - 錯誤的情況“透明” - “灰色” - “透明” - “灰色” - “灰色” - “灰色”但我希望它像:情況2 - 正確的情況“透明” - “灰色” - “透明” - “灰色” - “透明” - “灰色” - “透明”有誰知道如何編輯我的循環以獲得如上所述的結構?<div class="row page-margin pb-5">    <?php    $term = get_field('product_category'); if( $term ): $cat = esc_html( $term->slug ); endif;    $args = array( 'post_type' => 'product', 'product_cat' => $cat);    $loop = new WP_Query( $args );    $product_id = get_the_id();    $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 ($c & 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++; endwhile; wp_reset_query(); // Remember to reset ?></div>這是錯誤情況的圖像(情況1):錯誤的情況圖片
查看完整描述

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 的倍數

希望這有幫助!


查看完整回答
反對 回復 2022-09-12
?
POPMUISE

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 ?>



查看完整回答
反對 回復 2022-09-12
?
白衣非少年

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


查看完整回答
反對 回復 2022-09-12
  • 3 回答
  • 0 關注
  • 104 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號