Get Attachment URL By ID in WordPress

Used before category names. Blog WordPress WordPress Development

There is a pretty straightforward technique to do this if you’re seeking a way to obtain the URL of an attachment in WordPress by its ID. You can get an attachment’s URL from its ID using the WordPress function wp_get_attachment_url().

You must enter the ID of the attachment whose URL you want to access when using this function.

To get the attachment (media) URL by ID in WordPress, first, you need to find the ID. To do this, go to Media and click on the image. In the page URL, you will see the ID in front of the post.

In this example, the ID is 68.

Add the ID in the function to retrieve the URL for an attachment.


This will return the URL of the attachment with an ID of 68.

You can also use this function to get the URL of a specific size of an image attachment. To do this, you will need to pass in an additional parameter to the function. The parameter is an array containing the size you want. For example, if you wanted to get the URL of a thumbnail-size image, you would use the following code:

$url = wp_get_attachment_url( 68, array( ''size' => 'thumbnail' ) );

This will return the URL of the attachment with an ID of 68 in the thumbnail size.