Include captions

I’m using the Meta Box plugin, everything works fine. I’m using it to create gallery function on some custom post type, and I’m able to display the uploaded images using this code:

global $wpdb;
$meta = get_post_meta( get_the_ID(), 'meta_key', 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
        $src = wp_get_attachment_image_src( $att, 'full' );
        $src = $src[0];
        // Show image
        echo "<img src="https://wordpress.stackexchange.com/questions/47717/{$src}" />";
    }
}

The images are shown, but I want to also display the captions from each picture.
How can I do it?

1 Answer
1

  1. st Note:

    // Prepare the code for safety
    $images = $wpdb->get_col( $wpdb->prepare( "
        SELECT ID FROM %s
        WHERE post_type="attachment"
        AND ID IN ( %s )
        ORDER BY menu_order ASC
    ", $wpdb->posts, $meta ) );
    
  2. nd Note: Look into what you already got:

    // Inside the foreach loop
    echo '<pre>'; var_dump( $att ); echo '<pre>';
    
  3. rd Note: wp_get_attachment_metadata() gives you meta data. Just drop the post ID in there.

Leave a Comment