wp_get_attachment_link

Retrieve an attachment page link using an image or icon, if possible.

Description

(string) wp_get_attachment_link( (int) $id = 0, (string) $size = 'thumbnail', (constant) $permalink = false, (constant) $icon = false, (constant) $text = false, (string) $attr = '' ); 

Returns (string)

HTML content.

Parameters (6)

0. $id — Optional. (int)
Post ID or post object.
1. $size — Optional. (string) => 'thumbnail'
Image size. Accepts any valid image size, or an array of width and height values in pixels (in that order). Default thumbnail..
2. $permalink — Optional. (constant) => false
Optional, Whether to add permalink to image. Default false.
3. $icon — Optional. (constant) => false
Whether the attachment is an icon. Default false.
4. $text — Optional. (constant) => false
Link text to use. Activated by passing a string, false otherwise. Default false.
5. $attr — Optional. (string) => ''
Array or string of attributes. Default empty.

Usage

  1. if ( !function_exists( 'wp_get_attachment_link' ) ) { 
  2. require_once ABSPATH . WPINC . '/post-template.php'; 
  3.  
  4. // Optional. Post ID or post object. 
  5. $id = -1; 
  6. $size = 'thumbnail'; 
  7.  
  8. // Optional, Whether to add permalink to image. Default false. 
  9. $permalink = false; 
  10.  
  11. // Optional. Whether the attachment is an icon. Default false. 
  12. $icon = false; 
  13.  
  14. // Optional. Link text to use. Activated by passing a string, false otherwise. 
  15. // Default false. 
  16. $text = false; 
  17.  
  18. // Optional. Array or string of attributes. Default empty. 
  19. $attr = ''; 
  20.  
  21. // NOTICE! Understand what this does before running. 
  22. $result = wp_get_attachment_link($id, $size, $permalink, $icon, $text, $attr); 
  23.  

Defined (1)

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

/wp-includes/post-template.php  
  1. function wp_get_attachment_link( $id = 0, $size = 'thumbnail', $permalink = false, $icon = false, $text = false, $attr = '' ) { 
  2. $_post = get_post( $id ); 
  3.  
  4. if ( empty( $_post ) || ( 'attachment' !== $_post->post_type ) || ! $url = wp_get_attachment_url( $_post->ID ) ) { 
  5. return __( 'Missing Attachment' ); 
  6.  
  7. if ( $permalink ) { 
  8. $url = get_attachment_link( $_post->ID ); 
  9.  
  10. if ( $text ) { 
  11. $link_text = $text; 
  12. } elseif ( $size && 'none' != $size ) { 
  13. $link_text = wp_get_attachment_image( $_post->ID, $size, $icon, $attr ); 
  14. } else { 
  15. $link_text = ''; 
  16.  
  17. if ( '' === trim( $link_text ) ) { 
  18. $link_text = $_post->post_title; 
  19.  
  20. if ( '' === trim( $link_text ) ) { 
  21. $link_text = esc_html( pathinfo( get_attached_file( $_post->ID ), PATHINFO_FILENAME ) ); 
  22. /** 
  23. * Filters a retrieved attachment page link. 
  24. * @since 2.7.0 
  25. * @param string $link_html The page link HTML output. 
  26. * @param int $id Post ID. 
  27. * @param string|array $size Size of the image. Image size or array of width and height values (in that order). 
  28. * Default 'thumbnail'. 
  29. * @param bool $permalink Whether to add permalink to image. Default false. 
  30. * @param bool $icon Whether to include an icon. Default false. 
  31. * @param string|bool $text If string, will be link text. Default false. 
  32. */ 
  33. return apply_filters( 'wp_get_attachment_link', "<a href='" . esc_url( $url ) . "'>$link_text</a>", $id, $size, $permalink, $icon, $text );