Get Image Title from Attachment ID in WordPress

This has taken me ages to find, so thought it might be useful! It’s embarrassingly simple in the end…

I’m writing a gallery page for my new WordPress theme, and wanted to get the title of an image based on its attachment ID. I’ve already retrieved all the gallery image IDs using the following:

$gallery = get_post_gallery(get_the_ID(), false);
$galleryIDs = explode(',', $gallery['ids']);

It’s now really simple to get the image title using:

foreach( $galleryIDs as $imageID ) {
  get_the_title($imageID);
}

Thanks to WordPress StackExchange for the answer

[GARD]


COMMENTS