WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have some IDs of attachments (images, not from current post). How to I get the alt attribute for those images?

I have tried to look into wp_get_attachment_image_src and wp_get_attachment_metadata but couldn't find the alt attribute stored in neither of those

this is where I'm looking for the alt:

while(the_repeater_field('featured_pics')): //this is an array of IDs
    $image_id =  get_sub_field('featured_pic'); // this function gets an individual ID from array
    $image_attr = wp_get_attachment_image_src(get_sub_field('featured_pic'), 'medium');
    echo '<li class="showcase-pic"><a href="'.$image_attr[0].'" rel="lightbox">'.wp_get_attachment_image($image_id, 'thumbnail').'</a></li>'; 
share|improve this question
Look into $image_attr by either doing print_r($image_attr) or var_dump($image_attr) – Asko Jan 9 '12 at 19:43
no it won't work wp_get_attachment_src contains only url, height and width of the image, not meta attributes such as title or alt unfortunately – unfulvio Jan 10 '12 at 5:49

I haven't actually tested this, but came across the following code on my travels. Might this help?

get_post_meta($attachment_id, '_wp_attachment_image_alt', true)

share|improve this answer
no I'm sorry it doesn't seem to work – unfulvio Jan 10 '12 at 5:52
Very helpful for me on a related issue where I wanted a theme feature to work by grabbing the 'alt' attribute for an image. Thanks, @dunc! I also found these pages in the codex to be quite helpful: codex.wordpress.org/Function_Reference/get_post_meta codex.wordpress.org/Function_Reference/get_post_thumbnail_id (if you need the ID of a 'featured image') – kchjr Feb 14 '13 at 21:27

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.