WordPress wp_attachment_is_image() Function

Definition
In WordPress, wp_attachment_is_image() checks if an attachment is an image. Files in the Media Library can be images, videos, PDFs, and more.
It returns true for an image and false for anything else, so you use it in an if statement.
Syntax
A few ways to call wp_attachment_is_image().
<?php
if ( wp_attachment_is_image( $attachment_id ) ) {
echo 'This is an image.';
}
| Call | What it does |
|---|
wp_attachment_is_image() | Check the current attachment. |
wp_attachment_is_image( 15 ) | Check the attachment with ID 15. |
wp_attachment_is_image( $post ) | Check an attachment post object. |
It checks the file type of the attachment, not the page you are on.
Full declaration
wp_attachment_is_image( int|WP_Post $post = null ): bool
Parameters
| Parameter | Description |
|---|
$post | int|WP_Post. The attachment ID or object. Default is the current post. |
Return Value
| Return Value | Description |
|---|
| true | The attachment is an image. |
| false | The attachment is not an image, or it was not found. |
WordPress Examples
Three short examples. Each one shows the code and what it prints.
Example 1 — Check an ID
Test an attachment ID and print a message.
<?php
if ( wp_attachment_is_image( 15 ) ) {
echo 'Attachment 15 is an image.';
} else {
echo 'Attachment 15 is not an image.';
}
Attachment 15 is an image.
How It Works
15 is the attachment ID.- The function checks the file type.
- The message depends on the result.
Example 2 — Show an image only
Print the image only when the file is an image.
<?php
$id = get_the_ID();
if ( wp_attachment_is_image( $id ) ) {
echo wp_get_attachment_image( $id, 'medium' );
} else {
echo '<p>This file is not an image.</p>';
}
<img width="300" height="200" src="https://example.com/wp-content/uploads/2026/11/sunset-300x200.jpg" class="attachment-medium size-medium" alt="Sunset" decoding="async" />
How It Works
- The check protects
wp_get_attachment_image() from non-image files. - For a PDF we print a short message instead.
- This keeps the page clean.
Example 3 — Only list images
Go through the attachments of a post and keep the images.
<?php
$files = get_attached_media( '', get_the_ID() );
foreach ( $files as $file ) {
if ( wp_attachment_is_image( $file->ID ) ) {
echo esc_html( get_the_title( $file->ID ) ) . "\n";
}
}
How It Works
get_attached_media() returns all files of the post.- The
if skips PDFs, videos, and other files. - Only image titles are printed.
Frequently Asked Questions
It checks if an attachment in the Media Library is an image.
It returns true for an image, and false otherwise.
is_attachment() checks the page type. wp_attachment_is_image() checks the file type.
No, if you pass an attachment ID.
To avoid image functions on files that are not images, such as PDFs.
Browse all post functions
Open the functions index for simple guides with examples and FAQs.
Post Functions Index →About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
7 people found this page helpful