Reputation: 57
I am trying to add an alt attribute to the image when displayed on the website, but so far with no luck.
Thank you very much and here is the code I'm trying with:
<?php $featured = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), '1440_1712'); ?>
<?php $alt = get_post_meta($post->ID, '_wp_attachment_image_alt', true); ?>
<div class='img-half-wrap' style='background-image: url("<?php echo $featured[0]; ?>");' alt='<?php echo $alt; ?>'>
I've tried changing the $post->ID to $featured with no success. Any help is greatly appreciated.
Thank you
Upvotes: 0
Views: 1173
Reputation: 4136
You're using get_post_meta
on wrong object: what you want to get is the _wp_attachment_image_alt
meta from the media, not from the post.
So you should use this:
$alt = get_post_meta(get_post_thumbnail_id($post->ID), '_wp_attachment_image_alt', true);
Upvotes: 1