1

I have a line in my child theme as follows:

$bg_image = wp_get_attachment_url( get_post_meta(get_the_ID(), 'bg_image', true) );

It successfully provides me with the url of the image stored in the post meta, but what I would now like to do is specify a specific thumbnail size such as 'thumbnail' or 'medium'. The question was asked here but never answered.

Can anyone help?

1 Answer 1

1

You'll have to use different function for that.

wp_get_attachment_url returns a full URI for an attachment file and it doesn't know what type of attachment it is. It makes no sense to generate thumbnail for .zip od .txt files, so this function doesn't allow you to get thumbnail url.

On the other hand, wp_get_attachment_image_url knows that the file is an image, so you can tell it which size you want to get:

wp_get_attachment_image_url( int $attachment_id, string|array $size = 'thumbnail', bool $icon = false )

As you can see, second param is the size:

$size (string|array) (Optional) Image size to retrieve. Accepts any valid image size, or an array of width and height values in pixels (in that order).

Default value: 'thumbnail'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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