Home WordPress Links Functions get_attachment_link() WordPress get_attachment_link() Function Definition In WordPress, get_attachment_link() returns the URL of an attachment page . An attachment is a file you upload to the Media Library, such as an image or a PDF.
Do not mix it up with the file URL. The attachment link opens a WordPress page about the file. The file URL, from wp_get_attachment_url(), opens the file itself.
Syntax Pass the attachment ID, or call it inside an attachment template.
<?php echo esc_url( get_attachment_link( 57 ) ); ?> Call What it does get_attachment_link( 57 )The page URL of attachment 57. get_attachment_link()The page URL of the current attachment. get_attachment_link( 57, true )Keep the name part as a placeholder.
It returns the link. Print it with esc_url().
Full declaration get_attachment_link( int|WP_Post $post = null, bool $leavename = false ): string Parameters Parameter Description $postint|WP_Post. The attachment ID or object. Default is null, which means the current post. $leavenamebool. If true, the name part of the URL stays as a placeholder such as %postname%. Default is false.
Return Value Return Value Description string The URL of the attachment page.
WordPress Examples Three short examples. Each one shows the code and what it prints or does.
1. Get an attachment page URL 2. Link thumbnails to their attachment pages 3. Use it in an attachment template Example 1 — Get an attachment page URL Pass the ID of an uploaded image.
<?php
echo esc_url( get_attachment_link( 57 ) ); https://example.com/hello-world/sample-image/ How It Works 57 is the Media Library ID.WordPress builds the attachment page address. The path often includes the parent post name. Example 2 — Link thumbnails to their attachment pages Show images from a post and link each one to its own page.
<?php
$images = get_attached_media( 'image', get_the_ID() );
foreach ( $images as $image ) {
echo '<a href="' . esc_url( get_attachment_link( $image->ID ) ) . '">';
echo wp_get_attachment_image( $image->ID, 'thumbnail' );
echo '</a>';
} <a href="https://example.com/hello-world/photo-1/"><img width="150" height="150" src="https://example.com/wp-content/uploads/2026/11/photo-1-150x150.jpg" class="attachment-thumbnail size-thumbnail" alt="" /></a> How It Works get_attached_media() finds the images attached to the post.get_attachment_link() gives the page URL for each one.wp_get_attachment_image() prints the thumbnail.Example 3 — Use it in an attachment template In image.php or attachment.php, no ID is needed.
<?php
// image.php
echo '<p>Permalink: ' . esc_url( get_attachment_link() ) . '</p>'; <p>Permalink: https://example.com/hello-world/sample-image/</p> How It Works On an attachment page, the current post is the attachment. So get_attachment_link() needs no argument. esc_url() keeps the output safe.Frequently Asked Questions What does get_attachment_link() do? It returns the URL of the WordPress page for an uploaded file or image.
How is it different from wp_get_attachment_url()? get_attachment_link() gives the attachment page. wp_get_attachment_url() gives the direct URL of the file.
Does it print the link? No. It returns a string. Print it with echo and esc_url().
What if I do not pass an ID? It uses the current post. That works inside an attachment template.
What does leavename do? It keeps the name part of the URL as a placeholder. It is rarely needed in a theme.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
6 people found this page helpful
Helpful Share Copy link Suggestion