is_attachment()
Checks if the current page is an attachment page. Conditional Tag.
Checks if the current request is an attachment page — that is, a page for a single media file (image, document, etc.).
This function is part of the conditional tags and returns true, if the request matches the loaded attachment.
It is important to remember that it can be called after the main WordPress query has been run. If called before that, it will always return false and output a notice through _doing_it_wrong(), as conditional tags do not work yet.
The conditional tag is_singular() will also return true for attachment pages.
No Hooks.
Returns
true|false.
true— if the page is an existing attachment page.false— otherwise.
Usage
if( is_attachment( $attachment ) ){
// attachment page
}
- $attachment(int|string|int[]|string[])
The ID, title, slug of the attachment, or an array of such values.
Default: ''
Examples
#1 Checks if the current page is an attachment page:
<?php
if ( is_attachment() ) {
// code for attachment pages
}
else {
// code for other pages
}
?>
Notes
- Global. WP_Query.
$wp_queryWordPress Query object.
Changelog
| Since 2.0.0 | Introduced. |
is_attachment() is attachment code WP 6.9.1
function is_attachment( $attachment = '' ) {
global $wp_query;
if ( ! isset( $wp_query ) ) {
_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
return false;
}
return $wp_query->is_attachment( $attachment );
}