the_attachment_link()
Outputs an HTML A tag link to the file attached to the post (attachment) or the page of that file in the template.
Will output the message "Missing Attachment" if for some reason it was not possible to get the attachment link, or the attachment does not match the specified parameters.
The link will be shown if the page contains:
- An image attached to the post;
- The attachment's title (text). For attachments of types other than image.
Uses: wp_get_attachment_link()
1 time — 0.00291 sec (very slow) | 50000 times — 5.22 sec (fast) | PHP 7.1.2, WP 4.7.3
No Hooks.
Returns
null. Outputs data to the screen.
Usage
<?php the_attachment_link( $id, $fullsize, $deprecated, $permalink ); ?>
- $id(int)
- ID of the attachment whose link should be retrieved.
Default: ID of the current attachment (inside the attachments loop) - $fullsize(boolean)
- Parameter for image attachments. Can be:
false— will output a link to the image thumbnail (created by WordPress); if there is no thumbnail, a link to the original image size will be output;
true— will output a link to the full-size image.
Default: false - $deprecated(array)
- (parameter deprecated since version 2.3) Maximum width and height of the image or icon; if the image has a side larger than the one specified in this parameter, the link will not be displayed.
Default: not used - $permalink(boolean)
- Which URL to use for the link:
true— the URL in the engine's permalink structure;
false— simply, the URL to the file. Default.
Default: false
Examples
#1 Display a link to the attached full-size image (it will link to the image file itself):
<?php the_attachment_link( 4, true ); ?>
We get it:
<a href="https://example.com/wp-content/uploads/2011/03/yandex.png"> <img width="73" height="24" src="https://example.com/wp-content/uploads/2011/03/yandex.png" class="attachment-full size-full" alt=""> </a>
#2 Display a link to the attachment, which will lead to the attachment page, in the WP structure:
<?php the_attachment_link( 4, false, false, true); ?>
We get it:
<a href="https://example.com/yandex"> <img width="73" height="24" src="https://example.com/wp-content/uploads/2011/03/yandex.png" class="attachment-thumbnail size-thumbnail" alt=""> </a>
Changelog
| Since 2.0.0 | Introduced. |
the_attachment_link() the attachment link code WP 7.0
function the_attachment_link( $post = 0, $fullsize = false, $deprecated = false, $permalink = false ) {
if ( ! empty( $deprecated ) ) {
_deprecated_argument( __FUNCTION__, '2.5.0' );
}
if ( $fullsize ) {
echo wp_get_attachment_link( $post, 'full', $permalink );
} else {
echo wp_get_attachment_link( $post, 'thumbnail', $permalink );
}
}