wp_get_attachment_link() – Retrieve an attachment page link using an image or icon, if possible.

You appear to be a bot. Output may be restricted

Description

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

Usage

$string = wp_get_attachment_link( $id, $size, $permalink, $icon, $text, $attr );

Parameters

$id
( int|WP_Post ) optional – Optional. Post ID or post object.
$size
( string|int[] ) optional default: thumbnail – Optional. Image size. Accepts any registered image size name, or an array of width and height values in pixels (in that order). Default 'thumbnail'.
$permalink
( bool ) optional – Optional. Whether to add permalink to image. Default false.
$icon
( bool ) optional – Optional. Whether the attachment is an icon. Default false.
$text
( string|false ) optional – Optional. Link text to use. Activated by passing a string, false otherwise. Default false.
$attr
( array|string ) optional – Optional. Array or string of attributes. Default empty.

Returns

string HTML content.

Source

File name: wordpress/wp-includes/post-template.php


Lines:

1 to 47 of 47
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 );
}
 

 View on GitHub View on Trac