Getting image alt text in Wordpress slider

2.2k views Asked by At

Our web site is using the theme's slider.php on our front page. I'd like for the images displayed in that slider to use the alt text that is in the post meta (edited in the WP media library).

I've attempted to create a variable ($alt), and have that variable echoed when the img is called, but is coming up empty. Here's the relevant portion of slider.php:

$alt = get_post_meta($post->ID, '_wp_attachment_image_alt', true); 



<img src="<?php echo $thumbnail[0]; ?>" alt="<?php echo $alt[0]; ?>" />
    </a>

<?php else: ?>

` Any idea what I'm doing wrong?

2

There are 2 answers

1
McNab On

You have set the third argument as true which means the function get_post_meta returns a string;

http://codex.wordpress.org/Function_Reference/get_post_meta

Therefore the following should work instead;

<img src="<?php echo $thumbnail[0]; ?>" alt="<?php echo $alt; ?>" />
1
Hal Atkins On

Okay - so I've figured this out. The code above is trying to get the post meta from the thumbnail of the image - and, to my knowledge, the thumbnail image doesn't have meta data.

I've just created a custom field, and now the slider loop call thats field.