how do i retrieve and display the alt text of an image in wordpress?



PHP Snippet 1:

$img_id = get_post_thumbnail_id(get_the_ID());

PHP Snippet 2:

<?php $alt_text = get_post_meta($img_id , '_wp_attachment_image_alt', true); ?>
<h1 class="entry-title"><?php echo $alt_text; ?></h1>

PHP Snippet 3:

<?php
$thumb_id = get_post_thumbnail_id(get_the_ID());
$alt = get_post_meta($thumb_id, '_wp_attachment_image_alt', true);
if( $alt ):
    echo $alt;
endif;
?>

PHP Snippet 4:

$img_title = trim( strip_tags( $attachment->post_title ) );

$attr['title'] = $img_title;
$attr['alt'] = $img_title;

return $attr;

PHP Snippet 5:

<?php $thumb_id = get_post_thumbnail_id(get_the_ID()); 
$alt = get_post_meta($thumb_id, '_wp_attachment_image_alt', true); ?>
<img class="image" src="<?php echo $feature_image; ?>" alt="<?php echo $alt; ?>" >

PHP Snippet 6:

add_filter( 'render_block', function( $content, $block ) {
    if( 'core/image' !== $block['blockName'] )
        return $content;

    $alt = get_post_meta( $block['attrs']['id'], '_wp_attachment_image_alt', true );
    if( empty( $alt ) )
        return $content;

    // Empty alt
    if( false !== strpos( $content, 'alt=""' ) ) {
        $content = str_replace( 'alt=""', 'alt="' . $alt . '"', $content );

    // No alt
    } elseif( false === strpos( $content, 'alt="' ) ) {
        $content = str_replace( 'src="', 'alt="' . $alt . '" src="', $content );
    }

    return $content;
}, 10, 2 );

PHP Snippet 7:

<?php $image_id = get_post_thumbnail_id();
$image_alt = get_post_meta($image_id, '_wp_attachment_image_alt', TRUE);
$image_title = get_the_title($image_id);?>
<img alt="<?php echo $image_title;?>">