2016-06-23 4 views
0

現在、親の行にある2つのdivをphpでラップしていますが、これはうまく機能しています。PHPを使用して2つのdivのレイアウトを変更

しかし、私がする必要があるのは、3rd & 4th, 7th & 8thなどのdivが異なるレイアウトを持つようにすることです。これは現在、親行の2つのdivをすべてラップするコードです。

<?php 
    if(have_rows('products')) : 
     $counter = 1; 
     $i = 0; 
     ?> 
      <div class="o-table l-grid-half"> 
      <?php while(have_rows('products')) : 
      the_row(); 
      if ($i > 0 && ($i % 2 == 0)) { 
      // close row and open new row 
      ?> 
      </div> 
      <div class="o-table l-grid-half"> 
      <?php } ?> 
       <div class="o-table-cell l-grid-half__item "> 
        <div class="o-table-cell l-grid-half__item "> 
         <img src="<?php the_sub_field('product_image'); ?>"/> 
        </div> 
        <div class="o-table-cell o-table-vt l-grid-half__item o-full-sm-pd o-roman l-product__item o-bg-item-<?php echo($counter);?>"> 
         <h3 class="hd-panel-sm"><?php the_sub_field('product_heading'); ?></h3> 
         <?php the_sub_field('product_summary'); ?> 
         <a href="<?php the_sub_field('product_link'); ?>" class="link-std clearfix"><i class="icon-right-open"></i><span>Find out more</span></a> 
        </div> 
       </div> 
     <?php 
      $i++; 
      $counter++; 
     endwhile; 
    ?> 
     </div> 
    <?php endif; 
    ?> 

私が既存のコードにラップしようとしたことはすべて、スクリプトを壊しました。

私はhtmlのは、この(簡易版)のようになりたい最終目標:

<div class="o-table im-the-parent-row"> 
    <div class="o-table-cell im-the-child-div"> 
     <img src="img-on-the-left.jpg"/> 
     <p>text on the right</p> 
    </div> 
    <div class="o-table-cell im-the-child-div"> 
     <img src="img-on-the-left.jpg"/> 
     <p>text on the right</p> 
    </div> 
</div> 
<div class="o-table im-the-parent-row"> 
    <div class="o-table-cell im-the-child-div"> 
     <p>text on the left</p> 
     <img src="img-on-the-right.jpg"/> 
    </div> 
    <div class="o-table-cell im-the-child-div"> 
     <p>text on the left</p> 
     <img src="img-on-the-right.jpg"/> 
    </div> 
</div> 

更新コード:

<?php 
    if(have_rows('products')) : 
     $counter = 1; 
     $i = 0; 
     ?> 
      <div class="o-table l-grid-half"> 
      <?php while(have_rows('products')) : 
      the_row(); 
      if ($i > 0 && ($i % 2 == 0)) { 
      // close row and open new row 
      ?> 
      </div> 
      <div class="o-table l-grid-half"> 
      <?php } ?> 

      <!--Row 3&4, 7&8 have the image on the left --> 
      <?php if($i > 0 && (($i % 4 == 0) || (($i+1) % 4 == 0))) { ?> 
       <div class="o-table-cell l-grid-half__item "> 

        <div class="o-table-cell o-table-vt l-grid-half__item o-full-sm-pd o-roman l-product__item o-bg-item-<?php echo($counter);?>"> 
         <h3 class="hd-std-sm"><?php the_sub_field('product_heading'); ?></h3> 
         <?php the_sub_field('product_summary'); ?> 
         <a href="<?php the_sub_field('product_link'); ?>" class="link-std clearfix"><i class="icon-right-open"></i><span>Find out more</span></a> 
        </div> 
        <div class="o-table-cell l-grid-half__item "> 
         <img src="<?php the_sub_field('product_image'); ?>"/> 
        </div> 
       </div> 

      <?php } else {?> 
      <!--Row 1&2, 5&6 have the image on the right --> 
       <div class="o-table-cell l-grid-half__item "> 
        <div class="o-table-cell l-grid-half__item "> 
         <img src="<?php the_sub_field('product_image'); ?>"/> 
        </div> 
        <div class="o-table-cell o-table-vt l-grid-half__item o-full-sm-pd o-roman l-product__item o-bg-item-<?php echo($counter);?>"> 
         <h3 class="hd-std-sm"><?php the_sub_field('product_heading'); ?></h3> 
         <?php the_sub_field('product_summary'); ?> 
         <a href="<?php the_sub_field('product_link'); ?>" class="link-std clearfix"><i class="icon-right-open"></i><span>Find out more</span></a> 
        </div> 
       </div> 


      <?php } ?> 

     <?php 
     $i++; 
     $counter++; 
     endwhile;?> 
     </div> 
    <?php endif; 
    ?> 
+0

キーワード 'nth-child()'セレクタにはおそらくCSSを使用しているはずです。 – CBroe

答えて

0
    と一つの変数開始を取ります
  1. ループごとにインクリメント... 1 2 3 4
  2. 4に戻って1に戻る
  3. 3と4のときにカスタムコードを書くことができます。

(あなたが望むようにあなたが変数として、またはセッションに保存することができます)

1

$私は4の倍数であるとき、このコードは、あなたに「真」を与える:

($i > 0 && ($i % 4 == 0)) # -> 4rd, 8th, etc 

あなたがシフトその後、することができます一度に両方のケースを取得するには -

($i > 0 && ($i+1) % 4 == 0) # -> 3rd, 7th, etc 

EDIT:

$私は、第三及び第七itensを取得します
if($i > 0 && (($i % 4 == 0) || (($i+1) % 4 == 0))) { 
    /* do something... when $i = 3,4,7,8,11,12,... */ 
} 
+0

私は3番目と4番目の項目を取得する必要があります。どうすればいい? – probablybest

+0

whileループwhile:if($ i> 0 &&($ i%4 == 0)||(($ i + 1)%4 == 0))){私は= 3,4,7,8,11,12、... * /} –

+0

ありがとうございます、しかし、上記のコードは現在のところ、3と7を取得しています。私は3日と4日が必要です。その後7日と8日。 – probablybest

関連する問題