Php foreach循环包装每2件物品

Cod*_*ker 6 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 
        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>
Run Code Online (Sandbox Code Playgroud)

嗨堆垛机!

我需要一些关于如何包装循环元素的php帮助.我想<div class="row">.基本上把2个元素包装成一个<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>
Run Code Online (Sandbox Code Playgroud)

这只包装了我所有的循环项目,我希望div class = row只包装每2个项目

Axe*_*hor 11

你快到了:

    //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>"
Run Code Online (Sandbox Code Playgroud)


mon*_*nge 8

或这个:

<?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;
    }
}
Run Code Online (Sandbox Code Playgroud)

[UPDATE]

这让我很烦恼:一个奇怪的孩子可能会导致一排没有关闭标签.虽然大多数浏览器只会在渲染上添加标记,但您根本没有任何问题,但这仍然不是100%正确.

在奇数子计数上,你需要在foreach循环之后检查和关闭行,如下所示:

if($i==1){
    echo "</row>";
}
Run Code Online (Sandbox Code Playgroud)

如果循环后$ i == 1,那么这是一个奇怪的子项,并且必须关闭该行.

[/ UPDATE]


小智 -1

尝试这个

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

        if ($i==2) {
           //close row
        }
       $i++;
   }
Run Code Online (Sandbox Code Playgroud)