Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question

closed as off topic by Michael, kaiser, Stephen Harris, Rarst Oct 4 '12 at 17:25

Questions on WordPress Development Stack Exchange are expected to relate to WordPress within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

    
Are you willing to show only two images in one row? –  amit Aug 14 '12 at 13:29
    
Yes I will show only two images per row. (two block per row) I want two block per row even one is empty to prevent breaking the layout. –  pixelngrain Aug 14 '12 at 13:36

1 Answer 1

up vote 1 down vote accepted

ok, basicly. you need this logic

<?php
  if (($a % 2) == 1)
  { echo "$a is odd." ;}
  if (($a % 2) == 0)
  { echo "$a is even." ;}
?>

you'll loop like you do. but indeed keep a counter. and check accordingly. I changed your code without testing.

    <?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',)))

    //declare some counter var
       $counter = 0;

            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');
               // everytime raise your counter + 1
                $counter=  $counter+1; //you can use  $counter+=1; also 
                ?>
    // this part you only want every 2 rows 
<?php       
if (( $counter % 2) == 1){
 if ($counter > 1) {
  $uneven = true;     
      echo '</div>' ; // your closing div
}
   echo '<div class="mainrow">'; // opening div
 }

    //this part you want every time so no need to check it 
    ?>
                    <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 ($uneven && $counter == count($images)){ // $count is your amount of attach ?>
                    <div class="block">
                       <!--your empty image code -->
                        </a>
                    </div> <?php } ?>

</div>
     <?php }
    }
    ?>
share|improve this answer
    
I have tried this but the issue is it is not adding another block for the row if there is only one image. Means if there are three images than first row will have two block but second row will have only one block which is breaking my layout. I want two blocks per mainrow even if there is only one image for that row. –  pixelngrain Aug 14 '12 at 13:38
    
That is not clear in your question thou. The solution is easy thou, at the end of your loop you can check if you are even or uneven. If your count is uneven it means you had only one in your last row and you can add another subblock with some empty image then. I will edit above code for your convenience, do change your initial question so this is clear thou.. –  woony Aug 14 '12 at 13:46
    
I really appreciate your effort and help but I am not damn expert so getting trouble to understand what you have described here. I would appreciate if you can write few lines of code same as above you done. –  pixelngrain Aug 14 '12 at 13:50
    
@pixelngrain if so you can wrap the foreach loop into div with style (display: block;) that'll not break layout. –  amit Aug 14 '12 at 13:53
    
I edited the code. mind any syntax errors I haven't run the code. What happensi is you check if you are at the end, if you are at the end of your loop and you are at un eneven image it means there is only one and you can print an empty block. –  woony Aug 14 '12 at 13:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.