get_attachment_icon

Retrieve HTML content of icon attachment image element.

Description

(false|string) get_attachment_icon( (int) $id = 0, (constant) $fullsize = false, (bool) $max_dims = false ); 

Returns (false|string)

HTML content.

Parameters (3)

0. $id — Optional. (int)
Post ID.
1. $fullsize — Optional. (constant) => false
Optional, default to false. Whether to have full size image.
2. $max_dims — Optional. (bool) => false
Dimensions of image.

Usage

  1. if ( !function_exists( 'get_attachment_icon' ) ) { 
  2. require_once ABSPATH . WPINC . '/deprecated.php'; 
  3.  
  4. // Optional. Post ID. 
  5. $id = -1; 
  6.  
  7. // Optional, default to false. Whether to have full size image. 
  8. $fullsize = false; 
  9.  
  10. // Optional. Dimensions of image. 
  11. $max_dims = false; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = get_attachment_icon($id, $fullsize, $max_dims); 
  15.  

Defined (1)

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

/wp-includes/deprecated.php  
  1. function get_attachment_icon( $id = 0, $fullsize = false, $max_dims = false ) { 
  2. _deprecated_function( __FUNCTION__, '2.5.0', 'wp_get_attachment_image()' ); 
  3. $id = (int) $id; 
  4. if ( !$post = get_post($id) ) 
  5. return false; 
  6.  
  7. if ( !$src = get_attachment_icon_src( $post->ID, $fullsize ) ) 
  8. return false; 
  9.  
  10. list($src, $src_file) = $src; 
  11.  
  12. // Do we need to constrain the image? 
  13. if ( ($max_dims = apply_filters('attachment_max_dims', $max_dims)) && file_exists($src_file) ) { 
  14.  
  15. $imagesize = getimagesize($src_file); 
  16.  
  17. if (($imagesize[0] > $max_dims[0]) || $imagesize[1] > $max_dims[1] ) { 
  18. $actual_aspect = $imagesize[0] / $imagesize[1]; 
  19. $desired_aspect = $max_dims[0] / $max_dims[1]; 
  20.  
  21. if ( $actual_aspect >= $desired_aspect ) { 
  22. $height = $actual_aspect * $max_dims[0]; 
  23. $constraint = "width='{$max_dims[0]}' "; 
  24. $post->iconsize = array($max_dims[0], $height); 
  25. } else { 
  26. $width = $max_dims[1] / $actual_aspect; 
  27. $constraint = "height='{$max_dims[1]}' "; 
  28. $post->iconsize = array($width, $max_dims[1]); 
  29. } else { 
  30. $post->iconsize = array($imagesize[0], $imagesize[1]); 
  31. $constraint = ''; 
  32. } else { 
  33. $constraint = ''; 
  34.  
  35. $post_title = esc_attr($post->post_title); 
  36.  
  37. $icon = "<img src='$src' title='$post_title' alt='$post_title' $constraint/>"; 
  38.  
  39. return apply_filters( 'attachment_icon', $icon, $post->ID );