wp_get_attachment_link( int|WP_Post $id, 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

$id int|WP_Post Optional
Post ID or post object.
$size string|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'

$permalink bool Optional
Whether to add permalink to image.

Default: false

$icon bool Optional
Whether the attachment is an icon.

Default: false

$text string|false Optional
Link text to use. Activated by passing a string, false otherwise.

Default: false

$attr array|string Optional
Array or string of attributes.

Default: ''


Top ↑

Return

string HTML content.


Top ↑

Source

File: wp-includes/post-template.php. View all references

function wp_get_attachment_link( $id = 0, $size = 'thumbnail', $permalink = false, $icon = false, $text = false, $attr = '' ) {
	$_post = get_post( $id );

	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 ) );
	}
	/**
	 * 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  $id        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', "<a href='" . esc_url( $url ) . "'>$link_text</a>", $id, $size, $permalink, $icon, $text, $attr );
}

Top ↑

Hooks



Top ↑

Changelog

Changelog
Version Description
4.4.0 The $id parameter can now accept either a post ID or WP_Post object.
2.5.0 Introduced.

Top ↑

User Contributed Notes

  1. Skip to note 2 content
    Contributed by Codex

    ยดShow Medium Size Attachment
    The default image sizes of WordPress are “thumbnail”, “medium”, “large” and “full” (the image you uploaded). These image sizes can be configured in the WordPress Administration Media panel under Settings > Media.

    <?php 
        $id = 9; // ID of an attachment 
        echo wp_get_attachment_link( $id, 'medium' ); 
    ?>
  2. Skip to note 5 content
    Contributed by Codex

    Change Icon Directory
    WordPress can use media icons to represent attachment files on your blog and in the admin interface, if those icons are available. For images it returns the thumbnail. For other media types it looks for image files named by media type (e.g. audio.jpg) in the directory: wp-includes/images/crystal/.

    This example shows how you can change this directory to a folder called “images” in your theme: wp-content/themes/yourtheme/images. Create the folder and put the “media type images” in there. To tell WordPress the directory has changed put this in the current theme’s functions.php file:

    add_filter( 'icon_dir', 'wpdocs_theme_icon_directory' );
    add_filter( 'icon_dir_uri', 'wpdocs_theme_icon_uri' );
    
    /**
     * Get the path to the icon directory
     */
    function wpdocs_theme_icon_directory( $icon_dir ) {
    	return get_stylesheet_directory() . '/images';
    }
    
    /**
     * Get the URI of the icon directory
     */
    function wpdocs_theme_icon_uri( $icon_dir ) {
    	return get_stylesheet_directory_uri() . '/images'; 
    }
  3. Skip to note 6 content
    Contributed by mrengy

    I was trying to figure out how to link to the “attachment page” rather than the “media file”, and it took me way to long to make sense of the $permalink parameter. I only figured it out by one of the user-contributed examples. Perhaps you could make it more clear by making its name correspond to the terms used in admin when adding an image to a page / post: “link to” – with options of “media file”, “attachment page”, or “custom URL” (entered by the user).

You must log in before being able to contribute a note or feedback.