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
This commit is contained in:
rboren 2004-10-02 19:24:40 +00:00
parent 8fca0cb926
commit 0584402e9f
2 changed files with 130 additions and 13 deletions

View File

@ -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);

View File

@ -1604,16 +1604,52 @@ function wp_head() {
do_action('wp_head', '');
}
function is_single () {
function is_single ($post = '') {
global $wp_query;
return $wp_query->is_single;
if (! $wp_query->is_single) {
return false;
}
function is_page () {
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 ($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 () {
function is_author ($author = '') {
global $wp_query;
return $wp_query->is_author;
if (! $wp_query->is_author) {
return false;
}
function is_category () {
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 ($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 () {