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

I want every 2 items that's rendered out to be wrapped in a <div class="row">, however I can't figure it out. Can anyone help me?

So basically the row should wrap every 2 elements that is getting looped. I have been stuck on this forever... Hopefully anyone got better expertise than me hehe.

The div="row" should wrap the col-sm-6 and class="puff".

3
  • I formatted it for you sir. You already messing with code and you still have unformatted code in your Editor. Its horrible. Please use modern IDE which does auto Reformatting. Commented May 5, 2015 at 7:36
  • @jQuery.PHP.Magento.com thankyou. could you edit it so the loop works with the row? ;/ Commented May 5, 2015 at 7:40
  • check the updates in answer. Commented May 5, 2015 at 10:07

3 Answers 3

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 ;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Not necessary to reset the counter as 4%2 or 6%2 equal 0 too !
1

use proper logic

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

First check if its mod by 2 or not (Gives 0 value after MOD), then close div , open new.

Now increase counter . Because for the first time , i will be 0 , then you increment it and then you use logic. So in short counter shoul be incremented at the end only not in between before you do any operation/logic.

Updated

Use code as it is **: issue was you have i++ and your condition in 3rd else if which never executed. So took it outside All and just before 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 Comment

not working :( still not wrapping the looped elements
0

First set $i=0;

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.