WordPress – Get Post Thumbnail alt

If you are looking for the way to get post thumbnail/featured image alt value in WordPress, this post is for you.

<?php

function get_post_thumbnail_alt()  {
    $post_thumbnail_id = get_post_thumbnail_id();
    $post_thumbnail_alt = get_post_meta( $post_thumbnail_id, '_wp_attachment_image_alt', true );
    return $post_thumbnail_alt;
}

In the above code, we get the post thumbnail id of the current post and use get_post_meta function to get the alt value by passing _wp_attachment_image_alt to it as a meta key.

 

Use the function:

echo get_post_thumbnail_alt();