On this page
wp_get_attachment_link( int|WP_Post $post, string|int[] $size = 'thumbnail', bool $permalink = false, bool $icon = false, string|false $text = false, array|string $attr = '' ): string
Retrieves an attachment page link using an image or icon, if possible.
Parameters
$postint|WP_Post Optional-
Post ID or post object.
$sizestring|int[] Optional-
Image size. Accepts any registered image size name, or an array of width and height values in pixels (in that order). Default
'thumbnail'.Default:
'thumbnail' $permalinkbool Optional-
Whether to add permalink to image.
Default:
false $iconbool Optional-
Whether the attachment is an icon.
Default:
false $textstring|false Optional-
Link text to use. Activated by passing a string, false otherwise.
Default:
false $attrarray|string Optional-
Array or string of attributes.
Default:
''
Return
string HTML content.
Source
File: wp-includes/post-template.php. View all references
function wp_get_attachment_link( $post = 0, $size = 'thumbnail', $permalink = false, $icon = false, $text = false, $attr = '' ) {
$_post = get_post( $post );
if ( empty( $_post ) || ( 'attachment' !== $_post->post_type ) || ! wp_get_attachment_url( $_post->ID ) ) {
return __( 'Missing Attachment' );
}
$url = wp_get_attachment_url( $_post->ID );
if ( $permalink ) {
$url = get_attachment_link( $_post->ID );
}
if ( $text ) {
$link_text = $text;
} elseif ( $size && 'none' !== $size ) {
$link_text = wp_get_attachment_image( $_post->ID, $size, $icon, $attr );
} else {
$link_text = '';
}
if ( '' === trim( $link_text ) ) {
$link_text = $_post->post_title;
}
if ( '' === trim( $link_text ) ) {
$link_text = esc_html( pathinfo( get_attached_file( $_post->ID ), PATHINFO_FILENAME ) );
}
$link_html = "<a href='" . esc_url( $url ) . "'>$link_text</a>";
/**
* Filters a retrieved attachment page link.
*
* @since 2.7.0
* @since 5.1.0 Added the `$attr` parameter.
*
* @param string $link_html The page link HTML output.
* @param int|WP_Post $post Post ID or object. Can be 0 for the current global post.
* @param string|int[] $size Requested image size. Can be any registered image size name, or
* an array of width and height values in pixels (in that order).
* @param bool $permalink Whether to add permalink to image. Default false.
* @param bool $icon Whether to include an icon.
* @param string|false $text If string, will be link text.
* @param array|string $attr Array or string of attributes.
*/
return apply_filters( 'wp_get_attachment_link', $link_html, $post, $size, $permalink, $icon, $text, $attr );
}
Hooks
- apply_filters( 'wp_get_attachment_link',
string $link_html ,int|WP_Post $post ,string|int[] $size ,bool $permalink ,bool $icon ,string|false $text ,array|string $attr ) -
Filters a retrieved attachment page link.
Related
Uses
| Uses | Description |
|---|---|
| get_attachment_link() wp-includes/link-template.php | Retrieves the permalink for an attachment. |
| wp_get_attachment_image() wp-includes/media.php | Gets an HTML img element representing an image attachment. |
| wp_get_attachment_url() wp-includes/post.php | Retrieves the URL for an attachment. |
| get_attached_file() wp-includes/post.php | Retrieves attached file path based on attachment ID. |
| __() wp-includes/l10n.php | Retrieves the translation of $text. |
| esc_html() wp-includes/formatting.php | Escaping for HTML blocks. |
| esc_url() wp-includes/formatting.php | Checks and cleans a URL. |
| apply_filters() wp-includes/plugin.php | Calls the callback functions that have been added to a filter hook. |
| get_post() wp-includes/post.php | Retrieves post data given a post ID or post object. |
Used By
| Used By | Description |
|---|---|
| get_adjacent_image_link() wp-includes/media.php | Gets the next or previous image link that has the same post parent. |
| prepend_attachment() wp-includes/post-template.php | Wraps attachment in paragraph tag before content. |
| the_attachment_link() wp-includes/post-template.php | Displays an attachment page link using an image or icon. |
| gallery_shortcode() wp-includes/media.php | Builds the Gallery shortcode output. |
| wp_playlist_shortcode() wp-includes/media.php | Builds the Playlist shortcode output. |
Changelog
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/wp_get_attachment_link