Home WordPress Posts Functions wp_get_attachment_link() WordPress wp_get_attachment_link() Function 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().
<?php
echo wp_get_attachment_link( 15, 'thumbnail' ); Call What 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 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 Parameter Description $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 Value Description string The link HTML.
WordPress Examples Three short examples. Each one shows the code and what it prints.
1. Thumbnail link 2. Link to the attachment page 3. Use your own text Example 1 — Thumbnail link Return a thumbnail that links to the file.
<?php echo wp_get_attachment_link( 15, 'thumbnail' ); ?> <a href='https://example.com/wp-content/uploads/2026/11/sunset.jpg'><img width="150" height="150" src="https://example.com/wp-content/uploads/2026/11/sunset-150x150.jpg" class="attachment-thumbnail size-thumbnail" alt="" decoding="async" loading="lazy" /></a> How It Works The function returns the link and the image together. echo prints the result.The link opens the original file. Example 2 — Link to the attachment page Pass true as the third argument.
<?php echo wp_get_attachment_link( 15, 'thumbnail', true ); ?> <a href='https://example.com/hello-world/sunset/'><img width="150" height="150" src="https://example.com/wp-content/uploads/2026/11/sunset-150x150.jpg" class="attachment-thumbnail size-thumbnail" alt="" decoding="async" loading="lazy" /></a> How It Works The third argument true asks for the attachment page URL. The thumbnail stays the same. Visitors land on the attachment page. Example 3 — Use your own text Pass text to make a download link.
<?php echo wp_get_attachment_link( 22, 'thumbnail', false, false, 'Download the guide' ); ?> <a href='https://example.com/wp-content/uploads/2026/11/guide.pdf'>Download the guide</a> How It Works 22 is the ID of a PDF file.The fifth argument is the link text. The link goes straight to the file. Frequently Asked Questions What does wp_get_attachment_link() do? It returns an anchor tag for a file in the Media Library.
Does it print the link? No. It returns HTML. Use echo to print it, or use the_attachment_link().
How do I link to the attachment page? Pass true as the third argument.
How do I use my own link text? Pass your text as the fifth argument.
What does it show for a PDF? 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 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.
10 people found this page helpful
Helpful Share Copy link Suggestion