Search code examples
phparrayswordpresscaption

Wordpress Caption Array


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>

Solution

  • 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.