WordPress get_attachment_link() Function

Beginner
6 min read
Updated: Nov 2026
3 examples
Returns a URL

Definition

In WordPress, get_attachment_link() returns the URL of an attachment page. Every file you upload to the Media Library has its own page, and this function gives you the link.

It returns a string and does not print. Use echo esc_url() when you want to show it in a link.

Syntax

A few ways to call get_attachment_link().

WordPress
<?php
$url = get_attachment_link( $attachment_id );
echo esc_url( $url );
CallWhat it does
get_attachment_link()Link for the current attachment in The Loop.
get_attachment_link( 15 )Link for the attachment with ID 15.
get_attachment_link( 15, true )Same, but keep the %postname% part as a placeholder.

It returns the URL. It does not print anything.

Full declaration
WordPress
get_attachment_link( int|WP_Post $post = null, bool $leavename = false ): string

Parameters

ParameterDescription
$postint|WP_Post. The attachment ID or post object. Default is the current post.
$leavenamebool. Keep the post name placeholder in the URL when true. Default is false.

Return Value

Return ValueDescription
stringThe URL of the attachment page.

WordPress Examples

Three short examples. Each one shows the code and what it prints.

Example 1 — Get the link for an attachment ID

Pass an attachment ID and echo the URL.

WordPress
<?php
$link = get_attachment_link( 15 );
echo esc_url( $link );

How It Works

  1. 15 is the ID of an image in the Media Library.
  2. get_attachment_link() returns the page URL as a string.
  3. esc_url() makes the URL safe to print.

Example 2 — Make a text link

Wrap the URL in an anchor tag.

WordPress
<?php
$id = 15;
echo '<a href="' . esc_url( get_attachment_link( $id ) ) . '">';
echo 'View the sunset photo';
echo '</a>';

How It Works

  1. The URL comes from get_attachment_link().
  2. We print it inside the href attribute.
  3. The link text is ours.

Example 3 — Link every image of a post

Get all images attached to the current post and list their page links.

WordPress
<?php
$images = get_attached_media( 'image', get_the_ID() );
foreach ( $images as $image ) {
  echo '<a href="' . esc_url( get_attachment_link( $image->ID ) ) . '">';
  echo esc_html( get_the_title( $image->ID ) );
  echo "</a>\n";
}

How It Works

  1. get_attached_media() returns the images attached to the post.
  2. For each one we call get_attachment_link() with its ID.
  3. get_the_title() gives the image title for the link text.

Frequently Asked Questions

It returns the URL of the attachment page for a file in the Media Library.
No. The attachment page is a WordPress page. For the direct file URL use wp_get_attachment_url().
No. It returns a string. Use echo esc_url() to print it.
No, if you pass an attachment ID. Without an ID it uses the current post.
Use the_attachment_link() to print a link, or wp_get_attachment_link() to return one.

Browse all post functions

Open the functions index for simple guides with examples and FAQs.

Post Functions Index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful