2017-09-26 60 views
0

在每一個循環我想輪流我塊的佈局,所以它會是這樣:備用佈局爲每一個循環

環路1:左柱像,右列文本 迴路2:左欄文字,右列圖像

但到目前爲止,所有的即時管理至今每塊在同一側或第一塊是重複的,這是我到目前爲止的代碼:

任何幫助或一個點正確的方向將不勝感激

<?php $i = 0; 
foreach ($homepageblocks as $block): 
$i++; ?> 
<div class="row"> 
    <div class="container container-fluid"> 
     <div class="col-md-6"> 

       <h2><?php echo $block['title']; ?></h2> 
       <?php echo $block['content']; ?> 
       <a href="<?php echo $block['link']; ?>" class="blue--button">Read More</a> 
     </div> 
     <div class="col-md-6 nopadding"> 
      <img src="<?php echo '/uploads'.$img_path; ?>" alt="<?php echo $block['title']; ?>"/> 
     </div> 
    </div> 
</div> 
<?php if($i%2 == 0): ?> 
<div class="row"> 
    <div class="container container-fluid"> 
     <div class="col-md-6 nopadding"> 
      <img src="<?php echo '/uploads'.$img_path; ?>" alt="<?php echo $block['title']; ?>" /> 
     </div> 
     <div class="col-md-6"> 
      <h2><?php echo $block['title']; ?></h2> 
      <?php echo $block['content']; ?> 
      <a href="<?php echo $block['link']; ?>" class="blue--button">Read More</a> 
     </div> 
    </div> 
</div> 
<?php endif; ?> 
<?php endforeach; ?> 

回答

1

添加else分支到您的if

foreach ($homepageblocks as $block): 
    $i++;?> 
<div class="row"> 
    <div class="container container-fluid"> 
<?php 
    // one layout type 
    if($i%2 == 0):?> 
     <div class="col-md-6 nopadding"> 
      <img src="<?php echo '/uploads'.$img_path; ?>" alt="<?php echo $block['title']; ?>" /> 
     </div> 
     <div class="col-md-6"> 
      <h2><?php echo $block['title']; ?></h2> 
      <?php echo $block['content']; ?> 
      <a href="<?php echo $block['link']; ?>" class="blue--button">Read More</a> 
     </div> 

<?php 
    // another layout type 
    else:?> 
     <div class="col-md-6"> 
       <h2><?php echo $block['title']; ?></h2> 
       <?php echo $block['content']; ?> 
       <a href="<?php echo $block['link']; ?>" class="blue--button">Read More</a> 
     </div> 
     <div class="col-md-6 nopadding"> 
      <img src="<?php echo '/uploads'.$img_path; ?>" alt="<?php echo $block['title']; ?>"/> 
     </div> 
<?php 
    endif;?> 
    </div> 
</div> 
<?php 
endforeach; ?> 
+0

偉大的作品,謝謝! – Tom

相關問題