WordPress the_attachment_link() Function

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

Definition

In WordPress, the_attachment_link() prints a link to an attachment. For an image, the link shows a thumbnail (or the full image) that points to the file.

It is a quick way to show a file from the Media Library by its ID.

Syntax

A few ways to call the_attachment_link().

WordPress
<?php
the_attachment_link( 15 );
CallWhat it does
the_attachment_link( 15 )Thumbnail link for attachment 15.
the_attachment_link( 15, true )Link with the full-size image.
the_attachment_link( 15, false, false, true )Link to the attachment page instead of the file.
wp_get_attachment_link( 15 )The same, but returned as a string.

It prints the link. The third argument is old and not used, so pass false.

Full declaration
WordPress
the_attachment_link( int|WP_Post $post = 0, bool $fullsize = false, array|false $deprecated = false, bool $permalink = false ): void

Parameters

ParameterDescription
$postint|WP_Post. The attachment ID or object. Default is 0, which uses the current post.
$fullsizebool. true shows the full-size image. false shows a thumbnail. Default is false.
$deprecatedarray|false. Not used. Pass false.
$permalinkbool. true links to the attachment page. false links to the file. Default is false.

Return Value

Return ValueDescription
voidNothing is returned. The link is printed.

WordPress Examples

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

Example 1 — Thumbnail link

Print a thumbnail that links to the file.

WordPress
<?php the_attachment_link( 15 ); ?>

How It Works

  1. 15 is the attachment ID.
  2. A thumbnail is used as the link content.
  3. The link opens the full file.

Example 2 — Full-size image

Pass true to show the full image in the link.

WordPress
<?php the_attachment_link( 15, true ); ?>

How It Works

  1. The second argument true means full size.
  2. The image is shown at its original size.
  3. The link still points to the file.

Example 3 — Link to the attachment page

Use the fourth argument to link to the page, not the file.

WordPress
<?php the_attachment_link( 15, false, false, true ); ?>

How It Works

  1. The fourth argument true asks for the attachment page URL.
  2. The third argument is not used.
  3. The thumbnail is the same.

Frequently Asked Questions

It prints a link to an attachment. For images, the link shows a thumbnail or the full image.
Use wp_get_attachment_link().
Pass true as the second argument.
Pass true as the fourth argument.
It is old and not used. Pass false.

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.

14 people found this page helpful