Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8386429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:56:11+00:00 2026-06-09T17:56:11+00:00

I am trying to achieve result with below structure in foreach loop where after

  • 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T17:56:13+00:00Added an answer on June 9, 2026 at 5:56 pm

    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
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi all i am trying to achieve below query to return result from two
I have 4 images stacked on top of each other, see below: I'm trying
I have a while/for loop with a foreach loop inside. Reading the code will
What I am trying to achieve is to set a result transformer on a
im trying to achieve something but i dont really know how I have set
The below is query is similar to what I am trying to achieve but
I really can't figure this one out... I'm trying to achieve the following result
What I am trying to achieve is to be able to print the result
I am trying to achieve this glowing effect for a UILabel as shown below
Trying to achieve any moving effect while appending an element from one to another

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.