None None - 6 months ago 14
PHP Question

How to display 4 items in one row and 4 in second row and so on...?

So what i need is to have this vertical_align div where i display 4x col-md-3 and then display in next div vertical_align next 4x col-md-3 and so on?

foreach ($images as $image) {
$image_attributes = wp_get_attachment_url( $image->ID );
?>
<div class="vertical_align"">
<div class="col-md-3">
<a href="">
<img src="<?php echo $image_attributes ?>" alt=""/>
</a>
</div>
<?php
$count++;
?>
</div>
<?php }

Answer

See below code to close the div and open a new div for each 4 items.

<div class="vertical_align"">
<?php foreach ($images as $image) {
    $image_attributes = wp_get_attachment_url( $image->ID );?>
        <div class="col-md-3">
            <a href="">
                <img src="<?php echo $image_attributes ?>" alt=""/>
            </a>
        </div>
        <?php
             $count++;
             // if reaches 4 items close the div and open a new div
             if($count % 4 ==0){?>
                </div> 
                <div class="vertical_align"">
            <?php  }     
     }?>
</div>

You can change the value of to any number by replace 4 to any number.

Comments