一、页面类型判断函数汇总一览

//1.判断是否是文章页面
is_single();
//2.判断是否是具体文章(id=2)的页面
is_single('2')/is_single(2);
//3.判断是否是具体文章(标题为Beef Stew)的页面
is_single('');
//4.判断是否是具体文章(slug为beef-stew)的页面
is_single('beef-stew');
//5.判断是否是具体文章(id=2或者slug=’beef-stew’或者标题为Irish Stew)的页面
is_single(array(2,'beef-stew','Irish Stew'));
//6.是否为页面页(Page)
is_page(); 
//7.是否为分类页(Category/Archive)
is_category(); 
//8.是否是作者页面
is_author(); 
//9.是否为Tag存档页
is_tag(); 
//10.是否为指定日期存档页
is_date(); 
//11.是否为指定年份存档页
is_year(); 
//12.是否为指定月份存档页
is_month(); 
//13.是否为指定日存档页
is_day(); 
//14.是否为指定时间存档页
is_time(); 
//15.是否为存档页
is_archive(); 
//16.是否为搜索结果页
is_search(); 
//17.是否为 “404: Not Found” 错误页
is_404(); 
//18.Home/Category/Archive页是否以多页显示
is_paged(); 
//19.是否是嵌入式页面
is_embed()   
//20.是否为首页页面
is_front_page()  
//21.是否为作品类型的页面
is_post_type_archive("作品")  
//22.是否为附件页面
is_attachment()   
//23.是否为文章单页或独立页面或附件页面的其中一个
is_singular() 

二、页面判断函数应用场景

1.1.判断不同的页面设置不同的模板,并调应对应的模板

if     ( is_embed()          && $template = get_embed_template()          ) :
elseif ( is_404()            && $template = get_404_template()            ) :
elseif ( is_search()         && $template = get_search_template()         ) :
elseif ( is_front_page()     && $template = get_front_page_template()     ) :
elseif ( is_home()           && $template = get_home_template()           ) :
elseif ( is_post_type_archive() && $template = get_post_type_archive_template() ) :
elseif ( is_tax()            && $template = get_taxonomy_template()       ) :
elseif ( is_attachment()     && $template = get_attachment_template()     ) :
remove_filter('the_content', 'prepend_attachment');
elseif ( is_single()         && $template = get_single_template()         ) :
elseif ( is_page()           && $template = get_page_template()           ) :
elseif ( is_singular()       && $template = get_singular_template()       ) :
elseif ( is_category()       && $template = get_category_template()       ) :
elseif ( is_tag()            && $template = get_tag_template()            ) :
elseif ( is_author()         && $template = get_author_template()         ) :
elseif ( is_date()           && $template = get_date_template()           ) :
elseif ( is_archive()        && $template = get_archive_template()        ) :
else :
$template = get_index_template();
endif;
if ( $template = apply_filters( 'template_include', $template ) ) {
  include( $template );
} elseif ( current_user_can( 'switch_themes' ) ) {
  $theme = wp_get_theme();
  if ( $theme->errors() ) {
    wp_die( $theme->errors() );
  }
}
return;
endif;

1.2.判断不同的页面调用不同的css和js文件

<?php if (is_home()) { ?>
<link rel="stylesheet" href="<?php bloginfo('template_url');?>/assets/css/home.css"/>
<script src="<?php bloginfo('template_url');?>/assets/js/home.js"></script>
<?php } ?>

博主联系方式:

  • 微信:34419369
  • QQ: 34419369
  • 公众号:前方录
  • 有什么不懂的地方欢迎联系我,帮到你是我会很开心

Leave a Reply

邮箱地址不会被公开。 必填项已用*标注