When I test get_the_post_thumbnail
(also the_post_thumbnail
) on WordPress 3.5.1, the alt
attribute is added as it should be. If you look at the source, you will see that get_the_post_thumbnail
uses wp_get_attachment_image
which does add that alt
attribute. By default, that attribute is the image file name but it can be edited from wp-admin->Media
to be anything you want.
Either:
- A plugin or your theme has already altered the output of
get_the_post_thumbnail
, possibly via thepost_thumbnail_html
filter, though I did notice other filters in the code, or - Your slider or theme template doesn’t work the way you think it does. Perhaps it isn’t using
get_the_post_thumbnail
, for example.
It is difficult to say more, or to say exactly where this is going wrong, without seeing the relevant code.
Also, get_the_post_thumbnail
function is not a pluggable function so I have to assume you are trying to hack a Core file, which you should not be doing.
Edit:
In the latest bit of code you posted, you can see the section that generates the image tag:
<a href="https://wordpress.stackexchange.com/questions/97571/<?php the_permalink(); ?>"><img src="<?php echo $image ?>"/></a>
It isn’t WordPress at all, but a customized bit of code. That is where you need to add the alt
attribute.
<a href="https://wordpress.stackexchange.com/questions/97571/<?php the_permalink(); ?>"><img src="<?php echo $image ?>" alt="I am so alt it hurts!" /></a>
You can create the alt attribute manually or copy the code from wp_get_attachment_image
$default_attr = array(
'src' => $src,
'class' => "attachment-$size",
'alt' => trim(strip_tags( get_post_meta($attachment_id, '_wp_attachment_image_alt', true) )), // Use Alt field first
);
if ( empty($default_attr['alt']) )
$default_attr['alt'] = trim(strip_tags( $attachment->post_excerpt )); // If not, Use the Caption
if ( empty($default_attr['alt']) )
$default_attr['alt'] = trim(strip_tags( $attachment->post_title )); // Finally, use the title
Or cook up your own function for creating the alt
attribute.