Using WordPress ‘get_image_tag()’ PHP function

The get_image_tag() WordPress PHP function generates an HTML img tag for an image attachment, scaling it down if requested.

Usage

get_image_tag( $id, $alt, $title, $align, $size );

Example:

echo get_image_tag( 123, 'A beautiful sunset', 'Sunset', 'alignleft', 'medium' );

Output:

<img src="image_url" alt="A beautiful sunset" title="Sunset" class="alignleft size-medium" />

Parameters

  • $id (int) – Required. The attachment ID.
  • $alt (string) – Required. The image description for the alt attribute.
  • $title (string) – Required. The image description for the title attribute.
  • $align (string) – Required. Part of the class name for aligning the image.
  • $size (string|int) – Optional. The image size. Accepts any registered image size name, or an array of width and height values in pixels (in that order). Default is ‘medium’.

More information

See WordPress Developer Resources: get_image_tag()

Examples

Display an image with a custom size

Display an image attachment with a custom size of 300×200 pixels.

echo get_image_tag( 456, 'Flowers in a vase', 'Flowers', 'aligncenter', array( 300, 200 ) );

Display an image with the default medium size

Display an image attachment with the default medium size and aligned to the right.

echo get_image_tag( 789, 'Mountain landscape', 'Mountains', 'alignright', 'medium' );

Display a thumbnail image

Display a thumbnail image attachment without alignment.

echo get_image_tag( 987, 'Portrait of a person', 'Portrait', '', 'thumbnail' );

Display a large image aligned to the left

Display a large image attachment aligned to the left.

echo get_image_tag( 654, 'A beautiful cityscape', 'Cityscape', 'alignleft', 'large' );

Display a full-sized image without alignment

Display a full-sized image attachment without alignment.

echo get_image_tag( 321, 'An amazing waterfall', 'Waterfall', '', 'full' );