Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

i want to display the alt text for a featured image.

I've been using the the_post_thumbnail(); function to display the image

like this:

if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
    the_post_thumbnail('medium');
} 

Question: How do I display the Alt attribute for this image?

share|improve this question
    
To clarify, I want to display text under the image like a typical caption. In Fact, if I could get it to display a caption, that would be awesome. –  Nohl Mar 23 '12 at 19:26
    
add comment

3 Answers 3

the_post_thumbnail() accepts second parameter $attr, that's array of options, so to display alt for image use something like this:

 the_post_thumbnail( 'medium', array( 'alt' => 'Image alt' ) );
share|improve this answer
    
OK, that doesn't show the alt tag, it sets the alt tag to 'Image alt' ...it's still more than I had. TY for the help with array syntax :) –  Nohl Mar 23 '12 at 19:22
    
So you want display text under the images, right? –  Mamaduka Mar 23 '12 at 19:32
    
yes please. like a caption –  Nohl Mar 23 '12 at 21:18
add comment

something I tried a while ago - purely experimental, not widely tested - this will wrap the thumbnail and caption into a div similar to a wp-caption image: (this filter function needs to be added to functions.php of your theme)

// 27/01/2012 alchymyth
// show thumbnail with caption, if it is entered, wrapped with <div class="wp-caption thumb-caption"> ;
// show just the thumbnail otherwise
add_filter('post_thumbnail_html','add_post_thumbnail_caption',10,5);
function add_post_thumbnail_caption($html, $post_id, $post_thumbnail_id, $size, $attr) {        
if( $html == '' ) { return $html;
} else {
 $out = '';

  $thumbnail_image = get_posts(array('p' => $post_thumbnail_id, 'post_type' => 'attachment'));

  if ($thumbnail_image && isset($thumbnail_image[0])) {

  $image = wp_get_attachment_image_src($post_thumbnail_id, $size); 
  $t_width = $image[1] +0; // +0 here for no extra padding, needs to be considered in writing css - the default image caption uses +10;
  $class = $attr['class'];
    if($thumbnail_image[0]->post_excerpt) $out .= '<div class="wp-caption thumb-caption '.$class.'" style="width:'.$t_width.'px; ">';

    $out .= $html; 

    if($thumbnail_image[0]->post_excerpt) $out .= '<p class="wp-caption-text">'.$thumbnail_image[0]->post_excerpt.'</p></div>'; 
  }
return $out; 
}
/*css classes for this:
.wp-caption.thumb-caption { }
.wp-caption.thumb-caption img { }
.wp-caption.thumb-caption .wp-caption-text { }
*/
}

example output:

<div class="wp-caption thumb-caption " style="width:165px; "><img width="165" height="150" src="http://localhost/wordpress/wp-content/uploads/2010/05/birthday_cake-180x163.jpg" class="attachment-580x150 wp-post-image" alt="happy birthday" title="birthday_cake" /><p class="wp-caption-text">happy birthday</p></div>
share|improve this answer
    
This looks impressive. Did you put the function in the functions.php or directly in single.php? –  Nohl Mar 26 '12 at 15:30
    
as a filter, I put that into functions.php of a theme. –  Michael Mar 26 '12 at 16:57
add comment

This is what I ended up using on my page.php

It's enclosed by php operators. I'm using css to format it.

It seems a little too simple. What am I missing?

if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
    echo '<div class="featured-image">';
    the_post_thumbnail('medium');
    echo '<span id="caption">' . get_post( get_post_thumbnail_id() )->post_excerpt . '</span>';
    echo '</div>';
} 
share|improve this answer
    
'What am I missing?' does this mean it is not working as expected? (I just tried the code in single.php and it was working) have you added a caption under dashboard -> media -> library -> edit media for those images? –  Michael Mar 26 '12 at 17:10
    
My CSS was incomplete at the time of the post. My apologies. –  Nohl Aug 24 '12 at 18:43
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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