0

I want to set post title for alt and title Attribute in this code

what should I do?

<?php
if ( true == get_theme_mod( 'archive_featured_images', true ) ) :
    if ( has_post_thumbnail() && ( get_the_post_thumbnail() != '' ) ) :
        echo '<a class="featured-image" href="'. esc_url( get_permalink() ) .'" title="'. the_title_attribute( 'echo=0' ) .'">';
        echo '<span itemprop="image" itemscope itemtype="https://schema.org/ImageObject">';
        the_post_thumbnail( 'mudra-featured-image' );
        $mudra_image = wp_get_attachment_image_src( get_post_thumbnail_id(), 'mudra-featured-image' );
        echo '<meta itemprop="url" content="' . esc_url( $mudra_image[0] ) . '">';
        echo '<meta itemprop="width" content="' . esc_attr( $mudra_image[1] ) . '">';
        echo '<meta itemprop="height" content="' . esc_attr( $mudra_image[2] ) . '">';
        echo '</span>';
        echo '</a>';
    endif;
endif;
?>
1
  • the_post_thumbnail() will automatically include the image's alt text. You set it in the media library. Commented May 7, 2020 at 1:53

2 Answers 2

2

You can use the second parameter of the_post_thumbnail() function.

the_post_thumbnail( 'mudra-featured-image', ['alt' => get_the_title()] );

Another option, you can do this using the filter wp_get_attachment_image_attributes

add_filter('wp_get_attachment_image_attributes', function($attr){
    $attr['alt'] = get_the_title();
    return $attr;
});

Note, using the filter will affect other images printed using any function that depends on wp_get_attachment_image()

So, you may use the other two attributes passed to the callback function for more control. Example, if you want apply this only on the size "mudra-featured-image", your code will be something like this

add_filter( 'wp_get_attachment_image_attributes', function( $attr, $attachment, $size ){

    // Return the current $attr array as it is if the size is not "mudra-featured-image"
    if( 'mudra-featured-image' !== $size ){
        return $attr;
    }
    // If it is our targeted size then, change the "alt" attribute
    $attr['alt'] = get_the_title();
    return $attr;

}, 10, 3 );
1
  • Awesome. Simple and effective solutions especially for large sites👌
    – Jodyshop
    Commented Aug 2, 2022 at 23:26
0

You can use the third parameter of the function:

get_the_post_thumbnail($post, $size, $attr);

Example:

get_the_post_thumbnail(
    $post,
    '',
    array(
       'alt' => 'anything',
       'title' => 'anything',
    )
);

Parameters

$post

(int|WP_Post) (Optional) Post ID or WP_Post object. Default is global $post. Default value: null

$size

(string|int[]) (Optional) Image size. Accepts any registered image size name, or an array of width and height values in pixels (in that order). Default value: 'post-thumbnail'

$attr (string|array) (Optional) Query string or array of attributes. Default value: ''

You can get more details here: https://developer.wordpress.org/reference/functions/get_the_post_thumbnail/

Your Answer

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

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