Archived topic
Featured Image Title
3 replies · Started by Marin on June 16, 2020
Hi,
My featured image doesn't seem to have the 'image title' within the blog post. All other images inserted in the blog post seem to have the image title as I can see the text when I hover over the image (most browsers display title text of the image in a yellow tool tip that appears when you hover your mouse over the element). But for my featured images, I can't see this once the blog post is published.
I appreciate any help with this.
Thanks,
Marin
Hi there,
WordPress removed the title as a default attribute some time back.
You can add it back in with this PHP Snippet:
add_filter( 'wp_get_attachment_image_attributes', 'tl_add_img_title', 10, 2 );
// Add title attribute to featured image
function tl_add_img_title( $attr, $attachment = null){
$attr['title'] = get_post( $attachment->ID )->post_title;
return $attr;
}
Great! Thanks David.
You're welcome