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

Hi stackers!

I need some php help on how to wrap the looped elements. I want to wrap 2 elements in a <div class="row">. So basically <row> <item> <item> </row>

I have tried with some modulo as you can see, some if statements is still there. I set i as 0, and was trying to put <div class="row"> when 1 % 2 = 0 but found no solution on how to close the tags correctly ( should be closed after the second item) Any chance you could help out me as a novice php hacker?

EDIT:

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

This only wraps all my looped items, I want the div class=row to only wrap every 2 items

6 Answers 6

22

you're almost there:

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

2 Comments

please have a look at the last line of my code. You need to close the div after the loop and the else part is unnecessary.
it works perfectly. please mark this as answer. thanks.
9

Or this:

<?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;
    }
}

[UPDATE]

This bugs me: An odd count of children could possibly lead to a row without closing tag. While most Browser will just add the tag on render and you will have no problems at all, this is still not 100% correct.

On odd children count, you would want a check and a closing row after the foreach loop like this:

if($i==1){
    echo "</row>";
}

If $i == 1 after the loop, it was an odd count of children, and the row have to be closed.

[/UPDATE]

3 Comments

Great simple solution. I actually used this and it worked the best for an unknown amount of items including odd numbers. Good work.
THX, I think it is most readable, what makes code live longer than fancy geniuses code, which nobody can read.
Awesome! Clever!
3

You should use a for loop instead of a foreach loop like so:

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

if you may have an odd number of children you have to check if $i+1 < count($children) before printing it.

5 Comments

Don't use count($children) in a comparison, this will lead to performance issue. Write it like this for($i = 0, $count=count($children); $i < $count; $i+=2)
@ArturStępień Isn't that doing exactly the same thing but also assigning the count to a variable? I would have thought that you should assign the count variable outside of the for loop.
@RobertWent No it does not. What you have before first ; in the loop is processed before first interaction. What you have in the condition is compared every interaction, what you have at the end (after last ;) is processed after each interaction. So you should create the $count variable before the loop or do it like I did.
@ArturStępień I see it now, thanks for the clarification!
@RobertWent No problem man, one baby seal just go saved XD
1

This is in Wordpress but understands it and it will help you.

<?php $query = new WP_Query( array( 'post_type' => 'slides', 'order'=> 'DESC', 'post_status' => 'publish', 'posts_per_page' => -1) );
               $posts = $query->posts; 
               $numOfCols = 2;
                $rowCount = 0;
               if(!empty($posts)){ ?>

                   <div class="carousel-item <?php echo ($numOfCols-1==1)?'active':''; ?>">
                    <div class="row"> 

                      <?php foreach ($posts as $post) { ?>
                        <div class="col-md-6 pt-4 pb-0 " >
                           <h6 class="mb-2 text-uppercase"><b><a href="<?php echo get_permalink( $post->ID); ?>" target="_blank"><?php echo $post->post_title; ?></a></b></h6>
                           <span><?php echo get_the_excerpt($post->ID); ?></span><span class="float-right"><a href="<?php echo get_permalink( $post->ID); ?>" target="_blank"><i _ngcontent-ttx-c19="" class="material-icons icon-image-preview" style="position: relative;  top: 7px;">arrow_forward</i></a></span>
                        </div>

                      <?php 
                      $rowCount++; 
                      if($rowCount % $numOfCols == 0 && $rowCount != count($posts)) echo '</div></div><div class="carousel-item "><div class="row"> ';
                    } ?>

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

Comments

0
# Process every second item starting with the first one [0].
foreach ($array as $key => $value) {
    if (($key - 1) % 2 === 0) {
        continue;
    }

    # Do something here.

}

1 Comment

While this code may provide a solution to the question, it's better to add context as to why/how it works. This can help future users learn, and apply that knowledge to their own code. You are also likely to have positive feedback from users in the form of upvotes, when the code is explained.
-1

Try this

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

        if ($i==2) {
           //close 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.