When I try to get the caption or description from a image in wordpress it’s not working. I can’t find a real answer. I tried so many things but I don’t know where i have to put some of the code. I read that I had to put this in functions.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
);
}
$attachment_meta = wp_get_attachment(your_attachment_id);
and this in my loop:
<?php echo $attachment_meta['caption']; ?>
Like this:
<section>
<div class="container">
<div class="row">
<?php
if( class_exists('Dynamic_Featured_Image') ):
global $dynamic_featured_image;
global $post;
$featured_images = $dynamic_featured_image->get_featured_images( $post->ID );
if ( $featured_images ):
?>
<?php foreach( $featured_images as $images ): ?>
<div class="col-md-4 col-sm-4 col-xs-12">
<img src="<?php echo $images['full'] ?>" alt="" class="img-responsive">
<?php echo $attachment_meta['caption']; ?>
</div>
<?php endforeach; ?>
<?php
endif;
endif;
?>
</div>
</div>
</section>
What am I doing wrong?
You must call
wp_get_attachment()
function inside your loop like this: