I have an array of images.
In the array, for each image, I have whether the image is tall or wide.
I need to loop through all of the images, and pair a tall with wide into a div, then move to the next until they are all done.
I have something that sort of works using a foreach loop, but the problem is that some images get skipped … the loop stops when each image has gone through the loop, which is standard behavior for a “foreach” loop, from what I have read…
So now I am wondering if there is a different loop I should use, or should I put my foreach loop inside another loop so that it repeats until the images are used up?
Here is my loop so far, that does what I want, but leaves out several images:
$num_of_images = count($images);
$notins = array();
$order = 1;
$i = 0;
//start my loop
foreach ($images as $image) {
$id = $image->ID;
//check if image is in the notins array..skips if it is
if (!in_array($id, $notins)){
//find out if the image is tall or wide
$attach = wp_get_attachment_image_src( $id, 'full');
$height = $attach['2'];
$width = $attach['1'];
if ($height < $width) { $size = "wide" ;} else { $size = "tall" ;}
//get the link to the sized image
$preview_array = image_downsize( $image->ID, $size );
$img_preview = $preview_array[0]; // thumbnail or medium image to use for preview.
if( $order == 3 ){ //if previous slide was tall, get a wide slide
if($size == "wide") { $order = 1; array_push($notins, $id); $i++; ?>
<!-- - - - - - - - - - - Image - - - - - - - - - - - - - -->
<div class="image <?php echo $size; ?>" style="background: url(<?php echo $img_preview; ?>) no-repeat center !important; background-size: cover !important; max-width: 100%; ">
</div>
<!-- - - - - - - - - - - end Image - - - - - - - - - - - - - -->
</li><!--.slide --><?php
}
}
if( $order == 2 ){ //if previous slide was wide, get a tall slide
if($size == "tall" ) { $order = 1; array_push($notins, $id); $i++; ?>
<!-- - - - - - - - - - - Image - - - - - - - - - - - - - -->
<div class="image <?php echo $size; ?>" style="background: url(<?php echo $img_preview; ?>) no-repeat center !important; background-size: cover !important; max-width: 100%; ">
</div>
<!-- - - - - - - - - - - end Image - - - - - - - - - - - - - -->
</li><!--.slide --><?php
}
}
if( $order == 1) { //if this is the first slide in the div
if( !in_array($id, $notins) ) { $i++; array_push($notins, $id); ?>
<li class="the-slide gallery">
<!-- - - - - - - - - - - Image - - - - - - - - - - - - - -->
<div class="image <?php echo $size; ?>" style="background: url(<?php echo $img_preview; ?>) no-repeat center !important; background-size: cover !important; max-width: 100%; ">
</div>
<!-- - - - - - - - - - - end Image - - - - - - - - - - - - - --><?php
if( $i == 17 ){ ?> </div><!--.slide --> <?php }
if($size == "wide"){ $order = 2; } if($size == "tall"){ $order = 3; } } }
}
}
}
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
Heya all,
The main issue with your approach is that the
foreach
loop goes through each image only once and there’s no guarantee that the images will come in the sequence tall, wide, tall, wide, and so on.A better approach is to divide your images into two separate arrays, one for tall images and one for wide images, then pair them together.
Here’s a rough idea of how you could modify your code:
This is a simplified version of the code and you may need to modify it according to your needs. Please note that this code assumes that there are an equal number of tall and wide images. If this isn’t the case, you would need to add some additional logic to handle the situation where there are more images of one type than the other.
The
outputImage()
function used in this example should be a function where you put your code that generates the image HTML. You pass either a tall or a wide image to this function.This way, you are certain that each tall image will be paired with a wide image in sequence.
This question was answered by @sierracircle:
View the original comment