0

I am trying to achieve result with below structure in foreach loop where after every two images it will repeat entire structure.

I have some basic knowledge for something I can use eg. counter++; and than %2 but don't know syntax and how to use it for my code.

<?php
    function dt_attached($postid=0, $size='thumbnail', $attributes='', $linksize='full', $count=-1) {
        if ($postid<1) $postid = get_the_ID();
        if ($images = get_children(array(
            'post_parent' => $postid,
            'post_type' => 'attachment',
            'numberposts' => $count,
            'post_mime_type' => 'image',)))

            foreach($images as $image) {
                $attachment=wp_get_attachment_image_src($image->ID, 'thumbnail');
                $small_image = wp_get_attachment_image_src($image->ID, 'midium');
                $big_image = wp_get_attachment_image_src($image->ID, 'full');
                ?>

                <div class="mainrow">

                    <div class="block">
                        <a href='<?php echo $big_image[0]; ?>' class='cloud-zoom-gallery' title='Thumbnail 1' rel="useZoom: 'zoom1', smallImage: '<?php echo $small_image[0]; ?>' ">
                            <img src="<?php echo $attachment[0]; ?>" <?php echo $attributes; ?> />
                        </a>
                    </div>

                    <!--[I want to get two images in mainrow]-->
                    <div class="block">
                        <a href='<?php echo $big_image[0]; ?>' class='cloud-zoom-gallery' title='Thumbnail 1' rel="useZoom: 'zoom1', smallImage: '<?php echo $small_image[0]; ?>' ">
                            <img src="<?php echo $attachment[0]; ?>" <?php echo $attributes; ?> />
                        </a>
                    </div>

                </div>

                <?php //the_attachment_link($image->ID, false, true, false); ?>
        <?php }
    }
?>

So what I want is if there is more than two images it will repeat entire html structure. Thanks a lot for your help

4
  • So, what you're saying is that you only want the output that's in the foreach to output every n times? Commented Aug 14, 2012 at 13:06
  • I mean like I have 5 images so in first "mainrow" it will display first two images in "block" and 3rd and 4th will again create second "mainrow" and will be display in "block" than 5th image again create third "mainrow" and will generate two "block" again but one with 5th image and other will be blank (this is require for my structure) and this will go on Commented Aug 14, 2012 at 13:09
  • OK, so basically if $count % 2 == 1 you want a "placeholder" inserted instead of trying to place an image in the row? Commented Aug 14, 2012 at 13:11
  • Yes, reason is how many attachment will be there it's unknown so whatever attachments are there it will keep two block per mainrow Commented Aug 14, 2012 at 13:13

1 Answer 1

2

What I gathered from your comments is that you want to display two images per row, and if there's one extra image, to display a placeholder next to it in the final row.

All you need is a count of how many images there are, and whether it's an even or odd number. Then once you know you're at the last image (using an incrementing counter), you add the placeholder:

What your code doesn't do is place two images in one row. For that we need to take the modulo (%) of the counter as well.

<?php
$counter = 0;
$imgCount = count($images);

foreach ($images as $image) {
    $attachment  = wp_get_attachment_image_src($image->ID, 'thumbnail');
    $small_image = wp_get_attachment_image_src($image->ID, 'midium');
    $big_image   = wp_get_attachment_image_src($image->ID, 'full');

    if ($counter % 2 == 0): ?>
    <div class="mainrow">
    <?php endif; ?>

        <div class="block">
            <a href='<?php echo $big_image[0]; ?>' class='cloud-zoom-gallery' title='Thumbnail 1' rel="useZoom: 'zoom1', smallImage: '<?php echo $small_image[0]; ?>' ">
                <img src="<?php echo $attachment[0]; ?>" <?php echo $attributes; ?> />
            </a>
        </div>

    <?php if ($counter++ % 2 == 1): ?>
    </div>
    <?php endif; ?>

    <?php //the_attachment_link($image->ID, false, true, false); ?>
<?php
}

// Since (if there are an odd number of images) the loop may not close the <div>, 
// we have to make sure it does.
if ($counter % 2 == 0) {
    ?>
    <!-- placeholder goes here -->
    </div>
<?php
}
Sign up to request clarification or add additional context in comments.

8 Comments

Double-check the $images array using var_dump(). Make sure the IDs are all correct, etc.
The procedure is correct, you may just have to iron out some bugs as far as how the data is being accessed.
I am getting all images without this structure but when I am adding this html structure (where I am adding image tag twice in "block" div ) it is rendering same image twice (which is so obvious) and after if($count > 2) break; stops the rendering rest images.
OK edited - I forgot that the loop won't close the div unless there's an even number of images. You'll have to close it from outside if there's an odd number of images.
Okay thanks a lot waiting for that new code... oh its already there let me check
|

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.