get_the_attachment_link

Retrieve HTML content of attachment image with link.

Description

(string) get_the_attachment_link( (int) $id = 0, (constant) $fullsize = false, (constant) $max_dims = false, (constant) $permalink = false ); 

Returns (string)

Parameters (4)

0. $id — Optional. (int)
Post ID.
1. $fullsize — Optional. (constant) => false
Optional, default is false. Whether to use full size image.
2. $max_dims — Optional. (constant) => false
Max image dimensions.
3. $permalink — Optional. (constant) => false
Optional, default is false. Whether to include permalink to image.

Usage

  1. if ( !function_exists( 'get_the_attachment_link' ) ) { 
  2. require_once ABSPATH . WPINC . '/deprecated.php'; 
  3.  
  4. // Optional. Post ID. 
  5. $id = -1; 
  6.  
  7. // Optional, default is false. Whether to use full size image. 
  8. $fullsize = false; 
  9.  
  10. // Optional. Max image dimensions. 
  11. $max_dims = false; 
  12.  
  13. // Optional, default is false. Whether to include permalink to image. 
  14. $permalink = false; 
  15.  
  16. // NOTICE! Understand what this does before running. 
  17. $result = get_the_attachment_link($id, $fullsize, $max_dims, $permalink); 
  18.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/deprecated.php  
  1. function get_the_attachment_link($id = 0, $fullsize = false, $max_dims = false, $permalink = false) { 
  2. _deprecated_function( __FUNCTION__, '2.5.0', 'wp_get_attachment_link()' ); 
  3. $id = (int) $id; 
  4. $_post = get_post($id); 
  5.  
  6. if ( ('attachment' != $_post->post_type) || !$url = wp_get_attachment_url($_post->ID) ) 
  7. return __('Missing Attachment'); 
  8.  
  9. if ( $permalink ) 
  10. $url = get_attachment_link($_post->ID); 
  11.  
  12. $post_title = esc_attr($_post->post_title); 
  13.  
  14. $innerHTML = get_attachment_innerHTML($_post->ID, $fullsize, $max_dims); 
  15. return "<a href='$url' title='$post_title'>$innerHTML</a>";