2

I want to get all sizes of an attachment. I can use wp_get_attachment_metadata to do this. But it only return filenames, not file urls.

How can I do this?

This is a workaround, but doesn't take stuff such as CDNs into account:

$attachment_id = get_term_meta( $term->term_id, 'thumbnail_id', true);

$image = wp_get_attachment_metadata($attachment_id);
$image['id'] = $attachment_id;


foreach($image['sizes'] as $key => $image_size) {
   $image['sizes'][$key]['url'] = wp_upload_dir()['baseurl'] . '/' . dirname($image['file']) . '/' . $image_size['file'];
}
0

Once you already have the attachment ID, you can get the full path with get_attached_file:

$image_path = get_attached_file( $attachment_id );
1
  • Hi - I'd like the URL not the path
    – FooBar
    Feb 16 '20 at 11:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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