I would like to pull the image caption for each image into a carousel. I can successfully pull the alt tag, but I cannot get the caption.
This is what I have so far, but it is breaking.
<?php $args = array(
'post_type' => 'homepageslider',
'posts_per_page' => 1
);
$query = new WP_Query($args);
while ($query->have_posts()):
$query->the_post();
$meta = get_post_meta( get_the_ID( ), 'homepage_media_gallery', false );
if ( !is_array( $meta ) )
$meta = ( array ) $meta;
if ( !empty( $meta ) ) {
$meta = implode( ',', $meta );
$images = $wpdb->get_col( "
SELECT ID FROM $wpdb->posts
WHERE post_type = 'attachment'
AND ID IN ( $meta )
ORDER BY menu_order ASC
" );
foreach ( $images as $att ) {
// Get image's source based on size, can be 'thumbnail', 'medium', 'large', 'full' or registed post thumbnails sizes
$attachment_meta = wp_get_attachment($att);
$link = $attachment_meta['caption'];
$src = wp_get_attachment_image_src( $att, 'full' );
$alt = get_post_meta($att, '_wp_attachment_image_alt', true);
$src = $src[0];
// show caption
echo $link;
// Show image
echo "<div><img src='{$src}' class='project-images' />"; ?>
<?php if ($alt) : ?>
<div class='homepage-caption'>
<?php echo $alt; ?>
</div>
<?php endif ?>
<?php echo "</div>";
}
}
endwhile
?>
How do you get the caption of each image in the query? I am using the WP Meta Boxes plugin to pull the images.
UPDATE
Obmerk had it correct. There was no need for an SQL Query. This is what I have now that pulls alt, caption and description for each image.
<?php
$args = array( 'post_type' => 'attachment',
'orderby' => 'menu_order',
'order' => 'ASC',
'post_mime_type' => 'image' ,
'post_status' => null,
'numberposts' => null,
'post_parent' => $post->ID,
'exclude' => get_post_thumbnail_id()
);
$attachments = get_posts($args); ?>
<?php if ($attachments) : ?>
<?php foreach ( $attachments as $attachment ): ?>
<?php
$alt = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true);
$image_title = $attachment->post_title;
$caption = $attachment->post_excerpt;
$description = $attachment->post_content;
$src = wp_get_attachment_image_src( $attachment->ID, 'full' );
list($width, $height, $type, $attr) = getimagesize($src[0]); ?>
<div class="projectBlock">
<div class="projectCopy">
<h2><?php echo $alt ?></h2>
<h3><?php echo $caption ?></h3>
<p class='projectDescription'><?php echo $description ?></p>
</div>
<div class="projectImage">
<img src="<?php echo $src[0]; ?>" class="project-images
</div>
</div>
I am not sure how you use your metabox (and why??) , nor why would you do a direct SQL query in the code , but normally, this is how you would pull image (attachment) data :
Since atatchments in wp are actually a built in CPT ( actually a post) :
The attachment caption is actually the post excerpt
The attachment title is the post title
The attachment description is the post content .
so a full code woule be more along the lines of :