Site logo

Archived topic

Post title as ALT and TITLE tags for the featured image

5 replies · Started by Melkior on April 2, 2023

Viewing posts 1–6 of 6

Hi,
I have searched here but could not find a solution.
I need a code (to be placed in functions.php file of my child theme, or some other solution) that does what I have wroted in the topic title.

I have 200 images (with their own name and alt tag), hat i use as featured image for my 1000 posts. I want to set ALT and TITLE tags for that featured image in a way that it those tags print title of the posts.

Thank you!

Hi there,

can i see your site so i can see how the featured image is being displayed, i can then take a look at what you require.

Hi,
the url of the website is in "private information"

Try this PHP Snippet:

add_filter( 'wp_get_attachment_image_attributes', function( $atts ) {
    if ( get_post_type( get_the_ID() ) === 'post' ) {
        $atts['alt'] = the_title_attribute( 'echo=0' );
	$atts['title'] = the_title_attribute( 'echo=0' );
    }
    return $atts;
} );

Hi David,
Great, it works!

Thank you!

Glad to hear that!

This archived topic is closed to new replies.