Last active
May 22, 2021 06:33
-
-
Save jeroenlammerts/3d6bc3d6b246df43d77f86aeb2f538c7 to your computer and use it in GitHub Desktop.
Get Wordpress image in ACF style array.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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