Home » Functions » wp_get_attachment_url

Sure, here’s an explanation of the WordPress function wp_get_attachment_url.

Function: wp_get_attachment_url()


The wp_get_attachment_url function retrieves the URL of a specific attachment file. In WordPress, attachments refer to any files that are uploaded to the media library and attached to a post or page.

This function is particularly useful for developers who want to display the attachment file, such as an image, in their WordPress templates. By using the wp_get_attachment_url() function, developers can easily retrieve the URL of the attachment file and display it on their website.


The following code is an example of how to use the wp_get_attachment_url() function in a WordPress template:

$attachment_id = get_post_thumbnail_id();
$attachment_url = wp_get_attachment_url( $attachment_id );

<a href="<?php echo $attachment_url; ?>">
    <img src="<?php echo $attachment_url; ?>" alt="Thumbnail">

In this example, the wp_get_attachment_url() function is used to retrieve the URL of the post thumbnail image. The URL is then used to display the image in an anchor tag.

Overall, the wp_get_attachment_url() function is a useful tool for developers who want to retrieve the URL of an attachment file and display it on their WordPress website.

Learn More on WordPress.org

Register an account to save your snippets or go Pro to get more features.