最近在写个屏蔽插件,基本已经竣工了,其中就遇到个问题,就是我需要获取到当前页面的类型,比如分类页面,标签页,文章页,主页...等等,但是我发现wordpress没有内置的方法,于是只好手动撸了一个,代码如下:
/**
* 获取页面类型
* @return string 获取不到返回notfound,否则返回实际页面类型
*/
function yr_get_page_type()
{
global $wp_query;
$loop = 'notfound';
if ($wp_query->is_page) {
$loop = is_front_page() ? 'front' : 'page';
} else if ($wp_query->is_home) {
$loop = 'home';
} else if ($wp_query->is_single) {
$loop = ($wp_query->is_attachment) ? 'attachment' : 'single';
} else if ($wp_query->is_category) {
$loop = 'category';
} else if ($wp_query->is_tag) {
$loop = 'tag';
} else if ($wp_query->is_tax) {
$loop = 'tax';
} else if ($wp_query->is_archive) {
if ($wp_query->is_day) {
$loop = 'day';
} elseif ($wp_query->is_month) {
$loop = 'month';
} elseif ($wp_query->is_year) {
$loop = 'year';
} elseif ($wp_query->is_author) {
$loop = 'author';
} else {
$loop = 'archive';
}
} else if ($wp_query->is_search) {
$loop = 'search';
} else if ($wp_query->is_404) {
$loop = 'notfound';
}
return $loop;
}