WordPress is_attachment() Function

Beginner
4 min read
Updated: Nov 2026
3 examples
Conditional tag

Definition

In WordPress, is_attachment() returns true on an attachment page. This is the page WordPress can create for a file in the Media Library, such as an image or a PDF.

Use it in if statements to give attachment pages their own markup or to redirect them. An attachment page is also a single view, so is_singular() is true there too.

Syntax

Use it as the test of an if statement.

WordPress
if ( is_attachment() ) {
  // code for the true path
} else {
  // code for the false path
}
CallWhat it does
is_attachment()True on any attachment page.
is_attachment( 25 )True if the attachment page has the ID 25.
is_singular( 'attachment' )Another way to check the same page.

Many sites redirect attachment pages to the parent post. This tag helps you do that.

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

Parameters

ParameterDescription
$attachmentOptional. An attachment ID, title, slug, or an array of them. Default is empty, which means any attachment.

Return Value

Return ValueDescription
booltrue if the main query is for an attachment page, otherwise false.

WordPress Examples

Three short theme examples. Each one shows what prints on the true path and the false path.

Example 1 — Show the image on its page

Print the file only on attachment pages.

WordPress
<?php if ( is_attachment() ) {
  echo wp_get_attachment_image( get_the_ID(), 'large' );
} ?>

How It Works

  1. is_attachment() is true on the page of a media file.
  2. get_the_ID() gives the attachment ID.
  3. wp_get_attachment_image() returns the image tag, so we use echo.

Example 2 — Redirect to the parent post

Send visitors from the attachment page to the post that uses the file.

WordPress
function mytheme_attachment_redirect() {
  if ( is_attachment() ) {
    $parent = wp_get_post_parent_id( get_the_ID() );
    if ( $parent ) {
      wp_safe_redirect( get_permalink( $parent ), 301 );
      exit;
    }
  }
}
add_action( 'template_redirect', 'mytheme_attachment_redirect' );

How It Works

  1. template_redirect runs after the query is set, so the tag is safe.
  2. We read the parent post ID of the file.
  3. If there is a parent, the visitor is sent to it.

Example 3 — Check one attachment ID

Run special code for one file.

WordPress
<?php if ( is_attachment( 25 ) ) {
  echo 'This is the logo file page.';
} ?>

How It Works

  1. The first argument is the attachment ID.
  2. The tag is true only for that file.
  3. You can also pass a slug.

Frequently Asked Questions

It returns true or false (a bool). Use it inside an if statement.
No. The file URL points to the file itself. The attachment page is a WordPress page that shows the file inside your theme.
No. is_single() is for posts. Attachment pages return true for is_attachment() and is_singular().
WordPress looks for attachment.php, image.php, and similar files before it uses single.php.
Conditional tags need the main query. Call them in template files or on hooks such as wp or template_redirect, not directly at the top of functions.php.

Next: is_author()

Check if the current page is an author archive.

is_author() tutorial →

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.

10 people found this page helpful