From 0584402e9ff469a1fbc0512a03d1b7d28443253d Mon Sep 17 00:00:00 2001 From: rboren Date: Sat, 2 Oct 2004 19:24:40 +0000 Subject: [PATCH] Extend is_single(), is_page(), is_category(), and is_author() to accept an optional argument which designates a particular id, name, or nicename. git-svn-id: http://svn.automattic.com/wordpress/trunk@1728 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/classes.php | 47 ++++++++++++++++++- wp-includes/functions.php | 96 ++++++++++++++++++++++++++++++++++----- 2 files changed, 130 insertions(+), 13 deletions(-) diff --git a/wp-includes/classes.php b/wp-includes/classes.php index 83b154efa..32d7b519f 100644 --- a/wp-includes/classes.php +++ b/wp-includes/classes.php @@ -3,6 +3,8 @@ class WP_Query { var $query; var $query_vars; + var $queried_object; + var $queried_object_id; var $posts; var $post_count = 0; @@ -43,6 +45,8 @@ class WP_Query { unset($this->posts); unset($this->query); unset($this->query_vars); + unset($this->queried_object); + unset($this->queried_object_id); $this->post_count = 0; $this->current_post = -1; } @@ -522,7 +526,7 @@ class WP_Query { $this->posts = apply_filters('the_posts', $this->posts); $this->post_count = count($this->posts); if ($this->post_count > 0) { - $this->post = $posts[0]; + $this->post = $this->posts[0]; } update_post_caches($this->posts); @@ -553,6 +557,47 @@ class WP_Query { return $this->get_posts(); } + function get_queried_object() { + if (isset($this->queried_object)) { + return $this->queried_object; + } + + $this->queried_object = NULL; + $this->queried_object_id = 0; + + if ($this->is_category) { + global $cache_categories; + if (isset($cache_categories[$this->get('cat')])) { + $this->queried_object = $cache_categories[$this->get('cat')]; + $this->queried_object_id = $this->get('cat'); + } + } else if ($this->is_single) { + $this->queried_object = $this->post; + $this->queried_object_id = $this->post->ID; + } else if ($this->is_page) { + $this->queried_object = $this->post; + $this->queried_object_id = $this->post->ID; + } else if ($this->is_author) { + global $cache_userdata; + if (isset($cache_userdata[$this->get('author')])) { + $this->queried_object = $cache_userdata[$this->get('author')]; + $this->queried_object_id = $this->get('author'); + } + } + + return $this->queried_object; + } + + function get_queried_object_id() { + $this->get_queried_object(); + + if (isset($this->queried_object_id)) { + return $this->queried_object_id; + } + + return 0; + } + function WP_Query ($query = '') { if (! empty($query)) { $this->query($query); diff --git a/wp-includes/functions.php b/wp-includes/functions.php index bd06f0572..b6d6af16e 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -1604,16 +1604,52 @@ function wp_head() { do_action('wp_head', ''); } -function is_single () { - global $wp_query; +function is_single ($post = '') { + global $wp_query; - return $wp_query->is_single; + if (! $wp_query->is_single) { + return false; + } + + if (empty($post)) { + return true; + } + + $post_obj = $wp_query->get_queried_object(); + + if ($post == $post_obj->ID) { + return true; + } else if ($post == $post_obj->post_title) { + return true; + } else if ($post == $post_obj->post_name) { + return true; + } + + return false; } -function is_page () { - global $wp_query; +function is_page ($page = '') { + global $wp_query; - return $wp_query->is_page; + if (! $wp_query->is_page) { + return false; + } + + if (empty($page)) { + return true; + } + + $page_obj = $wp_query->get_queried_object(); + + if ($page == $page_obj->ID) { + return true; + } else if ($page == $page_obj->post_title) { + return true; + } else if ($page == $page_obj->post_name) { + return true; + } + + return false; } function is_archive () { @@ -1652,16 +1688,52 @@ function is_time () { return $wp_query->is_time; } -function is_author () { - global $wp_query; +function is_author ($author = '') { + global $wp_query; - return $wp_query->is_author; + if (! $wp_query->is_author) { + return false; + } + + if (empty($author)) { + return true; + } + + $author_obj = $wp_query->get_queried_object(); + + if ($author == $author_obj->ID) { + return true; + } else if ($author == $author_obj->user_nickname) { + return true; + } else if ($author == $author_obj->user_nicename) { + return true; + } + + return false; } -function is_category () { - global $wp_query; +function is_category ($category = '') { + global $wp_query; - return $wp_query->is_category; + if (! $wp_query->is_category) { + return false; + } + + if (empty($category)) { + return true; + } + + $cat_obj = $wp_query->get_queried_object(); + + if ($category == $cat_obj->cat_ID) { + return true; + } else if ($category == $cat_obj->cat_name) { + return true; + } else if ($category == $cat_obj->category_nicename) { + return true; + } + + return false; } function is_search () {