WordPress wp_get_attachment_link() Function

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

Definition

In WordPress, wp_get_attachment_link() returns an a tag for a file in the Media Library. For an image, the link holds a thumbnail. For other files, it holds the file title.

The link goes to the file, or to the attachment page if you ask for it.

Syntax

A few ways to call wp_get_attachment_link().

WordPress
<?php
echo wp_get_attachment_link( 15, 'thumbnail' );
CallWhat it does
wp_get_attachment_link( 15 )Link with a thumbnail, going to the file.
wp_get_attachment_link( 15, 'medium', true )Link going to the attachment page.
wp_get_attachment_link( 15, 'thumbnail', false, false, 'Download' )Use your own text as the link.
the_attachment_link( 15 )The same, but printed.

It returns the link HTML. Use echo to print it.

Full declaration
WordPress
wp_get_attachment_link( int|WP_Post $post = 0, string|int[] $size = 'thumbnail', bool $permalink = false, bool $icon = false, string|false $text = false, array|string $attr = '' ): string

Parameters

ParameterDescription
$postint|WP_Post. The attachment ID or object. Default is 0, the current post.
$sizestring|int[]. The image size to show. Default is thumbnail.
$permalinkbool. true links to the attachment page. false links to the file. Default is false.
$iconbool. true uses a media icon for non-images. Default is false.
$textstring|false. Link text. If false, an image or the title is used. Default is false.
$attrarray|string. Extra attributes for the link or image. Default is empty.

Return Value

Return ValueDescription
stringThe link HTML.

WordPress Examples

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

Example 1 — Thumbnail link

Return a thumbnail that links to the file.

WordPress
<?php echo wp_get_attachment_link( 15, 'thumbnail' ); ?>

How It Works

  1. The function returns the link and the image together.
  2. echo prints the result.
  3. The link opens the original file.

Example 2 — Link to the attachment page

Pass true as the third argument.

WordPress
<?php echo wp_get_attachment_link( 15, 'thumbnail', true ); ?>

How It Works

  1. The third argument true asks for the attachment page URL.
  2. The thumbnail stays the same.
  3. Visitors land on the attachment page.

Example 3 — Use your own text

Pass text to make a download link.

WordPress
<?php echo wp_get_attachment_link( 22, 'thumbnail', false, false, 'Download the guide' ); ?>

How It Works

  1. 22 is the ID of a PDF file.
  2. The fifth argument is the link text.
  3. The link goes straight to the file.

Frequently Asked Questions

It returns an anchor tag for a file in the Media Library.
No. It returns HTML. Use echo to print it, or use the_attachment_link().
Pass true as the third argument.
Pass your text as the fifth argument.
The title of the file, unless you pass your own text.

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.

10 people found this page helpful