Get Attachment URL By ID in WordPress

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().

What is an attachment?

When it comes to managing content on a WordPress website, there are many different tasks that need to be done. One of these tasks is managing attachments, such as images, videos, and documents. While WordPress makes it easy to upload and manage these attachments, it can be difficult to figure out how to get the URL for an attachment by its ID. This article will explain how to get the attachment URL by its ID in WordPress.

How to get an attachment URL by ID

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.

https://honarsystems.com/wp-admin/post.php?post=68&action=edit

In this example, the ID is 68.

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

wp_get_attachment_url(68)

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

http://honarsystems.com/wp-content/uploads/2021/11/image.jpg

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.

Shopping Cart