1

I am attempting to echo out my image captions in a Bootstrap Carousel. I have managed to get my images to display but unable to display my captions. I have attempted to use wp_get_attachment_metadata however nothing is being echoed out.

Here is the code I’m using.

<?php 

$args = array(
'post_type' => 'attachment',
'orderby' => 'menu_order',
'order' => ASC,
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID,
'exclude' => get_post_thumbnail_id()
);

$attachments = get_posts($args);

?>

            <!-- Wrapper for slides -->
                <!-- Wrapper for slides -->
            <div class="carousel-inner"> 
                <?php $i=0; foreach ($attachments as $attachment) { $imageInfo = wp_get_attachment_image_src($attachment->ID, 'full'); $imageCap = wp_get_attachment_metadata($attachment->ID) ?> 
                <div class="item <?php if ($i == 0) { echo 'active'; } ?> "> <img src="<?php echo $imageInfo[0]; ?>"/> 
                    <div class="carousel-caption">
                        <h4><?php echo $imageCap->caption;?></h4>
                    </div>
                </div>
                <?php $i++; } ?>
            </div>
            <!-- Controls -->
            <a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
                <span class="glyphicon glyphicon-chevron-left"></span>
            </a>
            <a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
                <span class="glyphicon glyphicon-chevron-right"></span>
            </a>
        </div>
      </div>
    </div>

1 Answer 1

1

Retrieve caption of image correct format is

 <?php $post_thumbnail_id = get_post_thumbnail_id( $post_id ); ?> 

<?php

function wp_get_attachment( $attachment_id ) {

    $attachment = get_post( $attachment_id );
    return array(
        'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ),
        'caption' => $attachment->post_excerpt,
        'description' => $attachment->post_content,
        'href' => get_permalink( $attachment->ID ),
        'src' => $attachment->guid,
        'title' => $attachment->post_title
    );
}

$at= wp_get_attachment($post_thumbnail_id);
print_r($at);//show array of all image detail like title , caption etc

?>
3
  • Could you possibly show me how I’d incorporate that into my code? Thank you for the help Commented Oct 15, 2014 at 13:26
  • 1
    in first line of code place <?php $post_thumbnail_id = get_post_thumbnail_id( $post->ID ); ?> //post id of your blog Commented Oct 16, 2014 at 4:21
  • put this code in end of my code to print img echo '<img href="'.$at['href'].'" src="'.$at['src'].' />'; //alt="'.$at['caption'].'" Commented Oct 16, 2014 at 4:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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