Languages: English • Italiano • 日本語 (Add your language)
Returns an HTML hyperlink to an attachment file or page, containing either
If no such attachment exists, the function returns the string Missing Attachment.
<?php wp_get_attachment_link( $id, $size, $permalink, $icon, $text, $attr ); ?>
<?php echo wp_get_attachment_link( 13 ); ?>
To get attachment IDs dynamically in a template, you would probably use something like get_children().
function returns like:
<a href='http://example.com/wp-content/uploads/2017/11/image-xyz.jpg'><img width="150" height="150" src="http://example.com/wp-content/uploads/2017/11/image-xyz-150x150.jpg" class="attachment-thumbnail size-thumbnail" alt="" >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' ); ?>Link Attachment to Post
This example will link the attachment to an attachment Page.
<?php $id = 9; // ID of an attachment echo wp_get_attachment_link( $id, 'thumbnail', true ); ?>Link Text to Attachment
This example returns an HTML hyperlink with "My link text" linking to an attachment file.
<?php $id = 9; // ID of an attachment echo wp_get_attachment_link( $id, '' , false, false, 'My link text' ); ?>Link Post Title to Attachment
This example returns an HTML hyperlink with the post title linking to an attachment file.
<?php $id = 9; // ID of an attachment echo wp_get_attachment_link( $id, '' ); ?>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', 'my_theme_icon_directory' ); add_filter( 'icon_dir_uri', 'my_theme_icon_uri' ); function my_theme_icon_directory( $icon_dir ) { return get_stylesheet_directory() . '/images'; } function my_theme_icon_uri( $icon_dir ) { return get_stylesheet_directory_uri() . '/images'; }Notes
Use wp_get_attachment_image() if you want the image only (not a hyperlink).
Change Log
Since: 2.5.0
Source File
wp_get_attachment_link() is located in
wp-includes/post-template.php
.Related
Attachment Functions:
get_children(), get attached media(), the_attachment_link(), get_attachment_link(), wp_get_attachment_link(), wp_get_attachment_image(), wp_get_attachment_image_src(), wp_get_attachment_url(), wp_get_attachment_thumb_file(), wp_get_attachment_thumb_url(), is_attachment(), wp_get_attachment_metadata()