2015-05-04 21 views
3
<div class="puffar"> 
     <?php 
     //Set up the objects needed 
     $my_wp_query = new WP_Query(); 
     $all_wp_pages = $my_wp_query->query(array('post_type' => 'page')); 

     //Get children 
     $children = ($post->post_parent) ? get_page_children($post->post_parent, $all_wp_pages) : get_page_children($post->ID, $all_wp_pages); 

     $i = 0; 
     //Build custom items 
     foreach($children as $child){ 
     $i++; 

     /* 
     if (i % 2 == 0) { ?> 

     <?php 
     } */ 
     ?> 

<div class="col-sm-6"> 
    <div class="puff"> 
     <div class="puff-image-holder"> 
      <?php echo get_the_post_thumbnail($child->ID, 'full'); ?> 
     </div> 
     <fieldset class="linedHeadline hlmedium"> 
      <legend><?php echo get_the_title($child->ID); ?></legend> 
     </fieldset> 
     <?php echo get_field("puff_introtext", $child->ID); ?> 
     <?php 
     $values = get_field('puff_lanktext', $child->ID); 
     if (get_field("popup_eller_lank", $child->ID) == "popup") { 
     ?> 
     <fieldset class="linedHeadline hlmedium"> 
      <legend><a class ="linktopage open-popup" href="<?php echo get_page_link($child->ID); ?>"><?php echo get_field("puff_lanktext", $child->ID); ?> </a></legend> 
     </fieldset> 
     <?php 
     } elseif (get_field("popup_eller_lank", $child->ID) == "extern") { 
     ?> 
      <fieldset class="linedHeadline hlmedium"> 
      <legend><a class ="linktopage" href="<?php echo get_field("puff_lank", $child->ID); ?>"><?php echo get_field("puff_lanktext", $child->ID); ?> </a></legend> 
     <?php 
     } else { } 
     ?> 

    </div> 
</div> 

<?php } ?> 
</div> 

嗨堆垛機!Php foreach循環每2個物品包裝

我需要一些關於如何包裝循環元素的PHP幫助。我想包裝2個元素在<div class="row">.所以基本上<row> <item> <item> </row>

我已經嘗試了一些模數,你可以看到,一些if語句仍然存在。我設置我爲0,並試圖把<div class="row"> 1%2 = 0時,但發現如何正確關閉標籤沒有解決方案(應在第二項後關閉) 任何機會,你可以幫助我作爲一個新手PHP黑客?

編輯:

<div class="puffar"> 
     <?php 
     //Set up the objects needed 
     $my_wp_query = new WP_Query(); 
     $all_wp_pages = $my_wp_query->query(array('post_type' => 'page')); 

     //Get children 
     $children = ($post->post_parent) ? get_page_children($post->post_parent, $all_wp_pages) : get_page_children($post->ID, $all_wp_pages); 

     $i = 0; 
     //Build custom items 
     echo "<div class='row'>"; 
     foreach($children as $child){ 
     ?> 

<div class="col-sm-6"> 
    <div class="puff"> 
     <div class="puff-image-holder"> 
      <?php echo get_the_post_thumbnail($child->ID, 'full'); ?> 
     </div> 
     <fieldset class="linedHeadline hlmedium"> 
      <legend><?php echo get_the_title($child->ID); ?></legend> 
     </fieldset> 
     <?php echo get_field("puff_introtext", $child->ID); ?> 
     <?php 
     $values = get_field('puff_lanktext', $child->ID); 
     if (get_field("popup_eller_lank", $child->ID) == "popup") { 
     ?> 
     <fieldset class="linedHeadline hlmedium"> 
      <legend><a class ="linktopage open-popup" href="<?php echo get_page_link($child->ID); ?>"><?php echo get_field("puff_lanktext", $child->ID); ?> </a></legend> 
     </fieldset> 
     <?php 
     } elseif (get_field("popup_eller_lank", $child->ID) == "extern") { 
     ?> 
      <fieldset class="linedHeadline hlmedium"> 
      <legend><a class ="linktopage" href="<?php echo get_field("puff_lank", $child->ID); ?>"><?php echo get_field("puff_lanktext", $child->ID); ?> </a></legend> 
     <?php 
     $i++; 
     if ($i % 2 == 0) { 
      echo "</div><div class='row'>"; 
     } 
     } else { } 
     ?> 
    </div> 
</div> 

<?php } ?> 
</div> 
</div> 

這隻包我所有的循環項目,我想在div類=行只包每2項

回答

0

試試這個

$i = 1; 
     //Build custom items 
     foreach($children as $child){ 
     if($i>2){ 
     $i =1; 
     } 

     if ($i==2) { 
      //close row 
     } 
     $i++; 
    } 
+0

有更多的代碼是什麼我嘗試更新firstpost。 – Codehiker

1

您應該使用一個for循環而不是foreach循環如下:

for($i = 0; $i < count($children); $i+=2) { 
    $child1 = $children[$i]; 
    $child2 = $children[$i+1]; 
    // print both 
} 

如果您可能有奇數個孩子,您必須在打印前檢查它是否爲$i+1 < count($children)

+0

不要在比較中使用'count($ children)',這會導致性能問題。像這樣寫出來($ i = 0,$ count = count($ children); $ i <$ count; $ i + = 2)' –

7

你幾乎沒有:

//Build custom items 
    echo "<row>"; 
    $i = 0; 
    foreach($children as $child) { 

     echo "item "; 

     $i++; 
     if ($i % 2 == 0 && $i != count($children)) { 
      echo "</row><row>"; 
     } 

    } 
    echo "</row>" 
+0

感謝您的努力!我通過編輯更新了firstpost。請糾正我! – Codehiker

+0

請看看我的代碼的最後一行。你必須在**循環和'else'部分之後關閉'div' **。 –

5

或者這樣:

<?php 
$i=0; 
foreach($children as $child){ 
    ++$i; 
    if($i==1){ 
     echo "<row>"; 
     echo "<item>$child</item>"; 
    } 
    if($i==2){ 
     echo "<item>$child</item>"; 
     echo "</row>" 
     $i=0; 
    } 
} 
+0

非常簡單的解決方案。我實際上使用了它,它對包括奇數在內的未知數量的項目起到了最好的作用。好工作。 –

+0

THX,我認爲它是最具可讀性的,是什麼讓代碼比沒有人能讀懂的天才代碼活得更久。 – mondjunge

+0

太棒了!聰明! – tinyCoder