Example 1 — Thumbnail link
Print a thumbnail that links to the file.
<?php the_attachment_link( 15 ); ?> How It Works
15is the attachment ID.- A thumbnail is used as the link content.
- The link opens the full file.

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.
A few ways to call the_attachment_link().
<?php
the_attachment_link( 15 ); | Call | What 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.
the_attachment_link( int|WP_Post $post = 0, bool $fullsize = false, array|false $deprecated = false, bool $permalink = false ): void | Parameter | Description |
|---|---|
$post | int|WP_Post. The attachment ID or object. Default is 0, which uses the current post. |
$fullsize | bool. true shows the full-size image. false shows a thumbnail. Default is false. |
$deprecated | array|false. Not used. Pass false. |
$permalink | bool. true links to the attachment page. false links to the file. Default is false. |
| Return Value | Description |
|---|---|
| void | Nothing is returned. The link is printed. |
Three short examples. Each one shows the code and what it prints.
Print a thumbnail that links to the file.
<?php the_attachment_link( 15 ); ?> 15 is the attachment ID.Pass true to show the full image in the link.
<?php the_attachment_link( 15, true ); ?> true means full size.Use the fourth argument to link to the page, not the file.
<?php the_attachment_link( 15, false, false, true ); ?> true asks for the attachment page URL.Open the functions index for simple guides with examples and FAQs.
14 people found this page helpful