I can get the image to appear fine in this bit of gallery code. However the flex slider gets its caption from a <p> tag (class="flex-caption") and I just need to echo out the image title inside that paragraph.

<?php if (!empty($flex_gallery)) {?>                    
    <div class="flex-container resize-620">
        <div class="flexslider">
            <ul class="slides">     
                foreach ($flex_gallery as $att) {
                    echo ( '<li>' . wp_get_attachment_image( $att , 'blog-full-width' ).
                    <p class="flex-caption">' . I NEED JUST THE IMAGE TITLE HERE . '</p>'); 
<?php } else { ?>       
    <?php the_post_thumbnail(
        array('class' => 'featured-full-width-top'));
<?php } ?>

3 Answers 3


You should be able to get the image title with wp_get_attachment_metadata(). It will return an array that contains useful stuff like the width and height, as well as the title.

  • Thanks, from looking at that I can see that it should work. However I was stuck for time so I wrote a quick jQuery hack to transfer the title from the image to the paragraph.
    – john joe
    Mar 5, 2012 at 12:53

you should be able to get it using get_post_title($att)


I follow a few ways to get Title for Images.

Foreach Loop: If I need separate Title from Image then:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post();

    $attachments = get_posts( array(
        'post_type'   => 'attachment',
        'numberposts' => -1,
        'post_status' => null,
        'post_parent' => $post->ID
    ) );

    if ( $attachments ) {
        foreach ( $attachments as $attachment ) {
            <li><?php echo wp_get_attachment_image( $attachment->ID, 'full' ); ?>
                <p><?php echo apply_filters( 'the_title', $attachment->post_title ); ?></p>
endwhile; endif; ?>

Second way for Alt Images:

<?php the_title_attribute($post->ID);?>

Third way:

<?php echo get_the_title( $thumbnail_id );?>

For more details please check Official Documentation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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