1

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>'; 
endwhile;   
2
  • Look into $image_attr by either doing print_r($image_attr) or var_dump($image_attr)
    – Asko
    Jan 9, 2012 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, 2012 at 5:49

1 Answer 1

4

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)

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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