WordPress is_attachment() Function

Beginner
6 min read
Updated: Nov 2026
3 examples
Returns true or false

Definition

In WordPress, is_attachment() checks if you are on an attachment page. An attachment page is the page that shows one uploaded file, such as an image.

It returns true or false, so you use it inside an if statement.

Syntax

A few ways to call is_attachment().

WordPress
<?php
if ( is_attachment() ) {
  echo 'This is an attachment page.';
}
CallWhat it does
is_attachment()True on any attachment page.
is_attachment( 15 )True on the attachment page with ID 15.
is_attachment( 'sunset' )True on the attachment with the slug sunset.
is_attachment( array( 15, 'beach' ) )True if it matches any item in the list.

It must be used after the main query has run, for example in a template file or inside a hook such as wp.

Full declaration
WordPress
is_attachment( int|string|int[]|string[] $attachment = '' ): bool

Parameters

ParameterDescription
$attachmentint|string|array. Attachment ID, title, slug, or an array of them. Default is empty, which matches any attachment.

Return Value

Return ValueDescription
trueThe page is an attachment page, and it matches the value you passed.
falseIt is not an attachment page.

WordPress Examples

Three short examples. Each one shows the code and what it prints.

Example 1 — Show a message on attachment pages

Check the page type and print a short note.

WordPress
<?php
if ( is_attachment() ) {
  echo '<p>You are viewing a media file.</p>';
}

How It Works

  1. is_attachment() is true on an attachment page.
  2. The message is printed only there.
  3. On normal posts nothing is printed.

Example 2 — Match a certain attachment

Pass an ID or slug to match one file.

WordPress
<?php
if ( is_attachment( 'sunset' ) ) {
  echo 'This is the sunset photo.';
} else {
  echo 'This is another page.';
}

How It Works

  1. The slug sunset is passed in.
  2. The check is true only on that attachment.
  3. The else part runs on all other pages.

Example 3 — Change the page title for attachments

Use it in a hook to change the document title.

WordPress
<?php
function mytheme_attachment_title( $title ) {
  if ( is_attachment() ) {
    $title['title'] = 'Media: ' . $title['title'];
  }
  return $title;
}
add_filter( 'document_title_parts', 'mytheme_attachment_title' );

How It Works

  1. The filter runs when the page title is built.
  2. is_attachment() is safe here because the query is ready.
  3. Only attachment pages get the Media: prefix.

Frequently Asked Questions

It checks if the current page is an attachment page, which is the page for one uploaded file.
It returns true or false.
Yes. Pass an ID, title, slug, or an array of them.
You called it before the main query ran. Use it in a template file or inside the wp hook.
is_attachment() checks the page type. wp_attachment_is_image() checks if a given file is an image.

Browse all post functions

Open the functions index for simple guides with examples and FAQs.

Post Functions Index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful