Skip to content

Instantly share code, notes, and snippets.

@jeroenlammerts
Last active May 22, 2021 06:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeroenlammerts/3d6bc3d6b246df43d77f86aeb2f538c7 to your computer and use it in GitHub Desktop.
Save jeroenlammerts/3d6bc3d6b246df43d77f86aeb2f538c7 to your computer and use it in GitHub Desktop.
Get Wordpress image in ACF style array.
<?php
function my_image($image_id) {
// Optional placeholder image
// if (! $image_id) {
// return get_field('placeholder', 'option');
// }
$img_sizes = get_intermediate_image_sizes();
$image = [
'url' => wp_get_attachment_image_src($image_id, 'full')[0],
'title' => get_the_title($image_id),
'alt' => get_post_meta($image_id, '_wp_attachment_image_alt', true),
'caption' => wp_get_attachment_caption($image_id),
'sizes' => [],
];
foreach ($img_sizes as $size) {
$src = wp_get_attachment_image_src($image_id, $size);
$image['sizes'][$size] = $src[0];
$image['sizes'][$size . '-width'] = $src[1];
$image['sizes'][$size . '-height'] = $src[2];
}
return $image;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment