wp_attachment_is_image()WP 2.1.0

Determines whether the specified post is WordPress attachment image. A conditional tag.

1 time — 0.000291 sec (fast) | 50000 times — 0.44 sec (very fast) | PHP 7.0.14, WP 4.7

No Hooks.

Return

true|false.

  • true, if the post MIME type is: jpg, jpeg, jpe, gif, png.
  • false in any other case.

Usage

wp_attachment_is_image( $post );
$post(int/WP_Post)
Attachment (post) ID or object.
Default: null (global $post)

Examples

0

#1 Check whether an attachment (a post) with ID 37 is an image

$id = 37;
if( wp_attachment_is_image( $id ) ){
	echo "Post with ID $id — is an image!";
}
else {
	echo "Post with ID $id is not an image.";
}

Changelog

Since 2.1.0 Introduced.
Since 4.2.0 Modified into wrapper for wp_attachment_is() and allowed WP_Post object to be passed.

wp_attachment_is_image() code WP 6.5.2

function wp_attachment_is_image( $post = null ) {
	return wp_attachment_is( 'image', $post );
}