From a9c661c00ebb419a9a83241396c7bb50eabd6790 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Wed, 22 Jan 2014 22:31:12 +0000 Subject: [PATCH] Let `is_attachment()` accept an $attachment parameter, similar to `is_page()` and `is_single()`. Adds Unit Tests for all 3. Props alex-ye for the initial patch. Fixes #24257. Built from https://develop.svn.wordpress.org/trunk@27016 git-svn-id: http://core.svn.wordpress.org/trunk@26893 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/query.php | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/wp-includes/query.php b/wp-includes/query.php index 8bf292e1fe..1b2866282d 100644 --- a/wp-includes/query.php +++ b/wp-includes/query.php @@ -175,9 +175,10 @@ function is_post_type_archive( $post_types = '' ) { * @since 2.0.0 * @uses $wp_query * + * @param mixed $attachment Attachment ID, title, slug, or array of such. * @return bool */ -function is_attachment() { +function is_attachment( $attachment = '' ) { global $wp_query; if ( ! isset( $wp_query ) ) { @@ -185,7 +186,7 @@ function is_attachment() { return false; } - return $wp_query->is_attachment(); + return $wp_query->is_attachment( $attachment ); } /** @@ -3387,10 +3388,30 @@ class WP_Query { * * @since 3.1.0 * + * @param mixed $attachment Attachment ID, title, slug, or array of such. * @return bool */ - function is_attachment() { - return (bool) $this->is_attachment; + function is_attachment( $attachment = '' ) { + if ( ! $this->is_attachment ) { + return false; + } + + if ( empty( $attachment ) ) { + return true; + } + + $attachment = (array) $attachment; + + $post_obj = $this->get_queried_object(); + + if ( in_array( $post_obj->ID, $attachment ) ) { + return true; + } elseif ( in_array( $post_obj->post_title, $attachment ) ) { + return true; + } elseif ( in_array( $post_obj->post_name, $attachment ) ) { + return true; + } + return false; } /**