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

  • SEARCH
  • Home
  • 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 8078051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:40:06+00:00 2026-06-05T15:40:06+00:00

From any specific Post, I am trying to display all images, with their corresponding

  • 0

From any specific Post, I am trying to display all images, with their corresponding captions and arrange them one after the other like:

-------------
|           |
|    Img1   |
|           |
-------------
  Caption 1    

-------------
|           |
|    Img2   |
|           |
-------------
  Caption 2        

-------------
|           |
|    Img3   |
|           |
-------------
  Caption 3        

THAT IS WHAT I WANT TO ACHIEVE.

The code:

<?php

$attachments = get_children( array(
'post_parent' => get_the_ID(), 
'order' => 'ASC', 
'post_mime_type' =>'image') );

$args = array(
'post_parent' => get_the_ID(),  
        'order' => 'ASC',
        'post_type' => 'attachment' ); 

$thumb_images = get_posts($args);


foreach ($attachments as $attachment_id => $attachment)  

foreach ($thumb_images as $thumb_image) 

{
{
  echo "<div class='image'>";
  echo wp_get_attachment_image( $attachment_id, 'full' );
  echo "</div>";
  echo "<div class='caption'>";  
  echo $thumb_image->post_excerpt;
  echo "</div>";
}
}

?>

If there are 3 images & their corresponding captions, this code displays 3 times each image, and each one with the 3 different captions. That is 9 images & 9 captions. At least the captions are in order, but images repeat.

-------------
|           |
|    Img1   |
|           |
-------------
 Caption 1    

-------------
|           |
|    Img1   |
|           |
-------------
 Caption 2        

-------------
|           |
|    Img1   |
|           |
-------------
 Caption 3


-------------
|           |
|    Img2   |
|           |
-------------
  Caption 1    

-------------
|           |
|    Img2   |
|           |
-------------
  Caption 2        

-------------
|           |
|    Img2   |
|           |
-------------
  Caption 3            


  ETC

IF THE CODE IS UPDATED TO:

   <?php

$attachments = get_children( array(
'post_parent' => get_the_ID(), 
'order' => 'ASC', 
'post_mime_type' =>'image') );

$args = array(
'post_parent' => get_the_ID(),  
        'order' => 'ASC',
        'post_type' => 'attachment' ); 

$thumb_images = get_posts($args);


foreach ($attachments as $attachment_id => $attachment)   {

foreach ($thumb_images as $thumb_image)     {}  


  echo "<div class='image'>";
  echo wp_get_attachment_image( $attachment_id, 'full' );
  echo "</div>";
  echo "<div class='caption'>";  
  echo $thumb_image->post_excerpt;
  echo "</div>";

}

?>

It displays the images without repetitions, but the caption belongs to the last image that was loaded and repeats the equivalent to the total amount of images associated with the post.

-------------
|           |
|    Img1   |
|           |
-------------
  Caption 3    

-------------
|           |
|    Img2   |
|           |
-------------
  Caption 3        

-------------
|           |
|    Img3   |
|           |
-------------
  Caption 3 

Any idea on how to write it correctly, so that you end with x times the number of images and x times the number of captions one after the other? Without repetitions.

Best regards.

Laura

  • 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-05T15:40:08+00:00Added an answer on June 5, 2026 at 3:40 pm

    Assuming that $attachments is the same size as $thumb_images:

    $i = 0;
    foreach ($attachments as $attachment_id => $attachment) {
        echo "<div class='image'>";
        echo wp_get_attachment_image($attachment_id, 'full');
        echo "</div>";
        echo "<div class='caption'>";  
        echo $thumb_images[$i]->post_excerpt;
        echo "</div>";
        $i++;
    }
    

    To have a better understanding of what’s in $attachments and $thumb_images, use the following code snippet for debugging:

    echo "<pre>";
    echo var_dump($attachments);
    echo var_dump($thumb_images);
    echo "</pre>";
    

    You’ll find that $thumb_images is an array.

    Q: What is the logic of $i++?

    $i++ is the post-increment operator.

    $i++ is really shorthand for $i = $i + 1, so for each iteration of the loop, the value of $i increments. Initially, $i = 0. Once $i++ is called, $i = 1. If $i++ is called again on another iteration, $i = 2 and so on and so forth.

    Instead of using a foreach loop, you can also use a for loop to iterate over arrays. For examples of the for loop (and correspondingly examples that use the $i++ concept), see PHP’s documentation for for.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any easy way to show all records from a specific mySQL row
I'm trying to retrieve all rows inserted during a specific month. SELECT dbo.Post.UserId, dbo.Post.Tags,
I need loging all HTTP request (from any application). I have Delphi 7.0. Anybody
I am trying to create an implicit conversion from any type (say, Int) to
I need to get a stack frame of a function from any PDB (All
When trying to load Microsoft.Xna.Framework.dll from any project, it throws a FileNotFoundException. The specified
I want to select all posts that contain a specific tag. I'm trying it
I have a website where people post links from Google+. I am trying to
I need a variable that is available from any function in my code, but
I would like to hide any text matching a pattern from any HTML page,

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.