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 7412015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:28:32+00:00 2026-05-29T06:28:32+00:00

I am trying to write a function that will output every other attachment caption

  • 0

I am trying to write a function that will output every other attachment caption and format each nicely within a div containing sequential IDs. However, anytime I add an echo and echo before and after a post_excerpt my code is completely breaking. Based on my HTML output the first echo seems to be ignored until the second time through the loop. The captions seem to post fine if I take out the echo div code.

             <?php while ( have_posts() ) : the_post(); ?>
             <?php
                $argsThumb = array(
                    'orderby' => 'menu_order',
                    'order' => 'ASC',
                    'post_type' => 'attachment',
                    'post_status' => null,
                    'post_parent' => $post->ID,
                    'include'  => $thumb_id
                );
               $thumb_images = get_posts($argsThumb);
               $currentThumb = 1;
               $captionID=1;
               foreach ($thumb_images as $thumb_image) {
                   if ($currentThumb % 2 == 0)
                        echo '<div class="caption'.$captionID++.'">';
                        echo $thumb_image->post_excerpt;
                        echo '</div>';
                        $currentThumb++;
               }
            ?>
           <?php endwhile; // end of the loop. ?>

Here is my HTML Output, in theory there should be 5 captions based on the fact that I have 10 images and the code is suppose to pull every other image caption:

    <div class="caption1"></div>
    BEFORE 3</div>
    <div class="caption2">CLIENT: ELYSIAN<br>AGENCY: IN HOUSE<br>PHOTOG: JEFF SCIORTINO<br><br><p>THIS IMAGE WAS PART OF A SERIES SHOT FOR THE ELYSIAN. I WAS APPROACHED TO ADD ELEMENTS THAT WERE NOT POSSIBLE TO CAPTURE IN CAMERA.</p></div>
    AFTER 2.</div>
    <div class="caption3">BEFORE 1</div>
  • 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-05-29T06:28:33+00:00Added an answer on May 29, 2026 at 6:28 am

    It looks like one problem might be that you’ve forgotten to wrap the if block in braces.

    Right now, what you have is basically this:

    foreach ($thumb_images as $thumb_image) {
        if ($currentThumb % 2 == 0) {
             echo '<div class="caption'.$captionID++.'">';
        }
    
        echo $thumb_image->post_excerpt;
        echo '</div>';
        $currentThumb++;
    }
    

    So, you may want to make it look like this, intead:

    foreach ($thumb_images as $thumb_image) {
        if ($currentThumb % 2 == 0) {
             echo '<div class="caption'.$captionID++.'">';
             echo $thumb_image->post_excerpt;
             echo '</div>';
             $currentThumb++;
        }
    }
    

    This above error is a really good reason to always include braces, even for single-line if blocks.

    However, I don’t think this will fully fix your issue. I suggest you look at your data again, as you can see that echo '</div>'; is happening five times. My guess is that the foreach ($thumb_images as $thumb_image) loop only has five attachments to grab.

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

Sidebar

Related Questions

I'm trying to write a function that, given two quadtrees representing images, will output
i am trying to write a function that will make DataRow[column] return nullable typed
I'm trying to write a function that will let me red-shift or blue-shift a
I am trying to write a function that will filter a list of tuples
I am trying to write a function that will: Disable the submit button when
I am trying to write a function that will randomly return an (x,y) co-ordinates
I'm trying to write a function that will return all the available cars for
I am trying to write a function that will convert a generic list to
I'm trying to write a short function that will let me quickly read in
I'm trying to write a regular expression (via Autohotkey's RegExReplace function) that will enforce

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.