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: https://develop.svn.wordpress.org/trunk@1728 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
d2ed07b4bf
commit
1268dcb29b
@ -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);
|
||||
|
@ -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 () {
|
||||
|
Loading…
Reference in New Issue
Block a user