8

i wanted to know if "Alt title" is needed like "Title" for post thumbnails and how can add it in my related post query... i'm using this code to get the post thumbnail:

<div class="td-module-thumb">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php if (has_post_thumbnail()) {
the_post_thumbnail(array(324,235));
} else {
echo '<img src="' . get_bloginfo('template_directory') . '/images/no-thumb/td_324x235.png" />';
}
?>
</a>
</div>

4 Answers 4

7

It is necessary to set an alt value for all of your images, in case a browser can not load the image or the visitor is using screen reader.

You have two options. Either use the featured image's caption (which can some times be blank) or use the post's title as alt.

You can get the caption by using get_post_meta(). Using it is as simple as this:

$alt = get_post_meta ( $image_id, '_wp_attachment_image_alt', true );
echo '<img alt="' . esc_html ( $alt ) . '" src="URL HERE" />';

It should be used in the loop though, or be passed the $post object or ID.

The alternative method is to use post's title as alt text. To do so, you can use:

echo '<img alt="' . esc_html ( get_the_title() ) . '" src="URL HERE" />';

You can set up a conditional and check if the thumbnail has a caption, and use it instead of post title, if available:

if ( $alt = get_the_post_thumbnail_caption() ) {
    // Nothing to do here
} else {
    $alt = get_the_title();
}

echo '<img alt="' . esc_html ( $alt ) . '" src="URL HERE"/>

UPDATE

If you wish to add the alt attribute directly to get_post_thumbnail(), you can pass it as an array to the function:

the_post_thumbnail( 'thumbnail', [ 'alt' => esc_html ( get_the_title() ) ] ); 
0
20

This example uses the alt text as it set in the media library. I prefer it because the behavior is more consistent for users who enter alt text using the typical WordPress options.

$thumbnail_id = get_post_thumbnail_id( $post->ID );
$alt = get_post_meta($thumbnail_id, '_wp_attachment_image_alt', true);   
the_post_thumbnail( 'full', array( 'alt' => $alt ) ); ?>
1
  • 4
    This should be accepted as the correct answer Jul 15, 2019 at 7:30
0

The simplest solution is the following code.

$thumbnail_id = get_post_meta( $post->ID, '_thumbnail_id', true );
$img_alt = get_post_meta ( $thumbnail_id, '_wp_attachment_image_alt', true );

echo $img_alt;

Or to complete the code:

$thumbnail_id = get_post_meta( $post->ID, '_thumbnail_id', true );
$img_alt = get_post_meta ( $thumbnail_id, '_wp_attachment_image_alt', true );

if(! $img_alt){
    $img_alt = get_the_title();
}
2
  • That's the same as Tom's except without using get_post_thumbnail_id(). What's wrong with get_post_thumbnail_id() ?
    – Rup
    Apr 6, 2021 at 23:41
  • @Rup It makes no difference. Only this is simpler and shorter. Apr 8, 2021 at 17:07
-2

You can use the following code in your WordPress Theme:

<div class="td-module-thumb">
    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
        <?php if (has_post_thumbnail()) {
            echo '<img src="' . the_post_thumbnail_url( array(324,235) ) . '" alt="any thing you want" title="Some Text" />';
        } else {
            echo '<img src="' . get_bloginfo('template_directory') . '/images/no-thumb/td_324x235.png" alt="any thing you want" title="Some Text" />';
        }
        ?>
    </a>
</div>
1
  • 1
    Welcome to WPSE. Please add an explanation to your answer: why could that solve the problem? How about the requested post thumbnail alt title?
    – Sven
    Jun 19, 2017 at 8:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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