Php foreach循环每隔2行包裹一行

时间:2015-05-05 07:22:40

标签: php wordpress twitter-bootstrap

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

我希望每个渲染出的2个项目都包含在<div class="row">中,但我无法理解。任何人都可以帮助我吗?

所以基本上这行应该包含每个循环的元素。我一直坚持这个...希望任何人都有比我更好的专业知识呵呵。

div="row"应该包裹col-sm-6class="puff"

3 个答案:

答案 0 :(得分:1)

使用正确的逻辑

    if ($i % 2 == 0) { 
        echo "</div><div class='row'>";
    }
    $i++;

首先检查其mod是否为2(在MOD之后给出0值),然后关闭div,打开新的。

现在增加反击。因为第一次,我将为0,然后你增加它,然后你使用逻辑。因此,在执行任何操作/逻辑之前,简而言之,最终只能在两端之间递增。

<强>更新

使用代码原样**:问题是你有i ++和你的条件在第三个,如果从未执行过。所以把它带到了All之外,就在foreach之前。

<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

                } else {
                }
                ?>
            </div>
        </div>
    <?php
        if ($i % 2 == 0) {
            echo "</div><div class='row'>";
        }

        $i++;
    } ?>
</div>
</div>

答案 1 :(得分:1)

$i = 0;
foreach ($children as $child) {
    $i++;

    // your code goes here 
    if($i % 2 == 0) { 
        echo "</div><div class='row'>";
        // reset the counter to 0 
        $i = 0 ;
    }
}

答案 2 :(得分:0)

首先设置$ i = 0;

if ($i % 2 == 0) { 
        echo "</div><div class='row'>";
    }
    $i++;