The get_attachment_icon() WordPress PHP function retrieves the HTML content of an icon attachment image element.
Usage
get_attachment_icon($id, $fullsize, $max_dims);
Custom Example:
Input:
get_attachment_icon(123, true, array(100, 100));
Output:
<img src="path-to-image.jpg" width="100" height="100" alt="Image Alt Text">
Parameters
$id
(int) – Optional. The Post ID of the attachment.$fullsize
(bool) – Optional. Whether to display the full size image. Default: false.$max_dims
(array) – Optional. The dimensions of the image (width, height). Default: false.
More information
See WordPress Developer Resources: get_attachment_icon()
Examples
Display an attachment icon with default settings
This example retrieves the attachment icon with default settings (thumbnail size and dimensions).
$attachment_id = 123; echo get_attachment_icon($attachment_id);
Display a full-size attachment icon
This example retrieves the full-size attachment icon.
$attachment_id = 123; echo get_attachment_icon($attachment_id, true);
Display an attachment icon with custom dimensions
This example retrieves the attachment icon with custom dimensions.
$attachment_id = 123; $dimensions = array(200, 200); echo get_attachment_icon($attachment_id, false, $dimensions);
Display all attachment icons of a post
This example retrieves and displays all attachment icons associated with a post.
$post_id = get_the_ID(); $attachments = get_children(array('post_parent' => $post_id, 'post_type' => 'attachment', 'post_mime_type' => 'image')); foreach ($attachments as $attachment) { echo get_attachment_icon($attachment->ID); }
Display an attachment icon with a link to the full-size image
This example retrieves the attachment icon and wraps it with a link to the full-size image.
$attachment_id = 123; $image_url = wp_get_attachment_url($attachment_id); $icon_html = get_attachment_icon($attachment_id); echo '<a href="' . $image_url . '">' . $icon_html . '</a>';