Using WordPress ‘get_attachment_innerHTML()’ PHP function

The get_attachment_innerHTML() WordPress PHP function retrieves the HTML content of an image element.

Usage

To use the get_attachment_innerHTML() function, you’ll need to provide the attachment ID, whether to display the full-sized image (optional), and the maximum dimensions of the image (optional). Here’s an example:

echo get_attachment_innerHTML(123, true, array(200, 200));

Parameters

  • $id (int) – Optional. The Post ID of the attachment.
  • $fullsize (bool) – Optional. Whether to display the full-sized image. Default is false.
  • $max_dims (array) – Optional. Dimensions of the image, as an array with two elements: width and height. Default is false.

More information

See WordPress Developer Resources: get_attachment_innerHTML

Examples

Display the full-sized image

In this example, we will display the full-sized image of the attachment with ID 123.

echo get_attachment_innerHTML(123, true);

Display the image with custom dimensions

In this example, we will display the image of the attachment with ID 123 and set its dimensions to 300×200 pixels.

echo get_attachment_innerHTML(123, false, array(300, 200));

Display the image using the default dimensions

In this example, we will display the image of the attachment with ID 123 using the default dimensions.

echo get_attachment_innerHTML(123);

Display the image with a maximum width

In this example, we will display the image of the attachment with ID 123 and set its maximum width to 400 pixels.

echo get_attachment_innerHTML(123, false, array(400, 0));

Display the image with a maximum height

In this example, we will display the image of the attachment with ID 123 and set its maximum height to 300 pixels.

echo get_attachment_innerHTML(123, false, array(0, 300));