the_attachment_link()WP 2.0.0

Display an attachment page link using an image or icon.

1 time — 0.00291 sec (very slow) | 50000 times — 5.22 sec (fast) | PHP 7.1.2, WP 4.7.3

No Hooks.

Return

null. Nothing (null).

Usage

the_attachment_link( $post, $fullsize, $deprecated, $permalink );
$post(int|WP_Post)
Post ID or post object.
$fullsize(true|false)
Whether to use full size.
Default: false
$deprecated(true|false)
Deprecated. Not used.
Default: false
$permalink(true|false)
Whether to include permalink.
Default: false

Examples

0

#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>
0

#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() code WP 6.5.2

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 );
	}
}