From 5bc3a62137860edd83a1187f8d23da0641391386 Mon Sep 17 00:00:00 2001 From: Andrew Nacin Date: Tue, 25 May 2010 15:36:23 +0000 Subject: [PATCH] Give is_singular the ability to provide awareness/context for custom post types. props ryan, scribu. Usage: is_singular('book') or is_singular( array( 'newspapers', 'books' ) ). fixes #12588. git-svn-id: https://develop.svn.wordpress.org/trunk@14887 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-includes/query.php | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/wp-includes/query.php b/wp-includes/query.php index 5395f3b1b2..74d4b66eb2 100644 --- a/wp-includes/query.php +++ b/wp-includes/query.php @@ -145,7 +145,7 @@ function is_attachment() { * @param string|int $author Optional. Is current page this author. * @return bool True if page is author or $author (if set). */ -function is_author ($author = '') { +function is_author($author = '') { global $wp_query; if ( !$wp_query->is_author ) @@ -180,7 +180,7 @@ function is_author ($author = '') { * @param string|array $category Optional. * @return bool */ -function is_category ($category = '') { +function is_category($category = '') { global $wp_query; if ( !$wp_query->is_category ) @@ -409,7 +409,7 @@ function is_month() { * @param mixed $page Either page or list of pages to test against. * @return bool */ -function is_page ($page = '') { +function is_page($page = '') { global $wp_query; if ( !$wp_query->is_page ) @@ -529,7 +529,7 @@ function is_single($post = '') { if ( !$wp_query->is_single ) return false; - if ( empty( $post) ) + if ( empty($post) ) return true; $post_obj = $wp_query->get_queried_object(); @@ -552,12 +552,18 @@ function is_single($post = '') { * @since 1.5.0 * @uses $wp_query * + * @param string|array $post_types Optional. Post type or types to check in current query. * @return bool */ -function is_singular() { +function is_singular($post_types = '') { global $wp_query; - return $wp_query->is_singular; + if ( empty($post_types) || !$wp_query->is_singular ) + return $wp_query->is_singular; + + $post_obj = $wp_query->get_queried_object(); + + return in_array($post_obj->post_type, (array) $post_types); } /** @@ -1237,7 +1243,7 @@ class WP_Query { * * @param string|array $query */ - function parse_query($query ) { + function parse_query($query) { if ( !empty($query) || !isset($this->query) ) { $this->init(); if ( is_array($query) )