Get Wordpress Featured Image "alt"
Here's a solution:
$thumbnail_id = get_post_thumbnail_id($post->ID);
$thumbnail_image = get_posts(array('p' => $thumbnail_id, 'post_type' => 'attachment'));
if ($thumbnail_image && isset($thumbnail_image[0])) {
echo '<span>'.$thumbnail_image[0]->post_excerpt.'</span>';
}
Or you can use your code, but instead of echoing $alt
directly you need to echo $alt->post_excerpt
.
Check if you get correct thumbnail id.
For me this code works perfect:
$thumbnail_id = get_post_thumbnail_id( $post->ID );
$alt = get_post_meta($thumbnail_id, '_wp_attachment_image_alt', true);