wordpress get attachment url by size
Wordpress: Get Attachment URL by Size
function get_attachment_url_by_size( $attachment_id, $size = 'thumbnail' ) {
$image_src = wp_get_attachment_image_src( $attachment_id, $size );
return $image_src[0];
}
Step 1: Define a function called
get_attachment_url_by_size
that takes two parameters:$attachment_id
and$size
, with a default value of'thumbnail'
.Step 2: Within the function, use the
wp_get_attachment_image_src
function to retrieve the image source (URL) for the specified attachment ID and size.Step 3: Return the URL of the image source by accessing the first element of the array returned by
wp_get_attachment_image_src
.
This function allows you to retrieve the URL of an attachment by specifying its ID and size within a WordPress environment.