Skip to content

Instantly share code, notes, and snippets.

@Robbertdk
Created August 19, 2014 10:28
Show Gist options
  • Save Robbertdk/82b3e83faf720a75b6d1 to your computer and use it in GitHub Desktop.
Save Robbertdk/82b3e83faf720a75b6d1 to your computer and use it in GitHub Desktop.
WordPress: Get the urls of all the attached image sizes
<?php
/**
* Get the urls of all the attached image sizes
*
* Array with image urls given when a correct image attachment ID is passed.
* similiar to core function wp_get_attachment_image() but for all the images.
* Biggest adventage is one db query for all intermediate_image_sizes
*
* @param int $post_id Attachment ID.
* @return array
*/
function get_all_attachment_image($id = 0){
$id = (int) $id;
$url_array = array();
if(!wp_get_attachment_metadata($id) && !wp_attachment_is_image($id)){
return false;
}
$img_meta = wp_get_attachment_metadata($id);
$img_sizes = $img_meta['sizes'];
$img_url_basename = wp_dirname(wp_get_attachment_url($id));
if(!$img_sizes){
return false;
}
foreach($img_sizes as $key => $image){
$url_array[$key] = $img_url_basename . '/' . $image['file'];
}
return $url_array;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment