How to use post title as featured image's alt text in Wordpress?
Asked Answered
M

2

6

How can I use post title as the alt text of that posts featured image in Wordpress?

please show me how/where can I tweak the WordPress core files to achieve that, since it seems that the theme I'm using now doesn't react in any way to plugins that re-write the alt tags of images...

Thank you!

Melloney answered 9/5, 2013 at 12:54 Comment(0)
H
8

No need to change WordPress core files:)

Just locate in your theme where the_post_thumbnail() function is used and change it to:

$title=get_the_title();
the_post_thumbnail( array(150, 150),array( 'alt' =>$title) );
Haskins answered 9/5, 2013 at 13:10 Comment(0)
M
3

user850010

You are absolutely right .

$title=get_the_title();
the_post_thumbnail( array(150, 150),array( 'alt' =>$title) );

this is usefull of you Alex please check it out.

Alex , if you want to display only specific image then use this code

<img src="'.wp_get_attachment_url( get_post_thumbnail_id($item->ID)).'" alt="'.$item->post_title.'" title="'.$item->post_title.'" />
Meridel answered 9/5, 2013 at 13:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.