wordpress页面类型判断函数汇总

文章目录[隐藏]

函数介绍

is_home() 判断是否为首页,并且显示的不是一个静态页面
is_front_page()判断是否为首页,包括首页显示的是一个静态页面
is_search()是否为搜索页
is_404()是否为404页面
is_category()是否为分类目录归档
is_author()是否为作者归档页面
is_day()是否为按天归档页面
is_month()是否为按月归档页面
is_year()是否为按年归档页面
is_tag()是否为标签归档页面
is_single()是否为文章页面
is_page()是否为页面单页
is_date()是否按日期归档页面,相当于包括is_day()、is_month()、is_year()
is_archive()是否为归档页面,相当于包括is_category()、is_author()、is_month()、is_day()、is_year()、is_tag()
is_singular()相当于is_single()||is_page()||is_attachment()
is_sticky() 置顶文章判断。
is_singular 用于判断单页
......更多见官网!

举例

比如sidebar里的文章标签,只会在某些页面显示:

<?php
//判断开始
if(is_front_page() || is_home() || is_page() || is_category()) {
//如果是首页、页面、文章目录才会显示
?>
	<div id="tag_cloud" class="widget">
	   <h3>文章标签</h3>
	   <?php wp_tag_cloud('smallest=8&largest=14&number=12&order=RAND'); ?>
	   <div class="action">
	      <a class="view-more" href="<?php bloginfo(url);?>/all-tags/">View All Tags »</a>
	   </div>
	</div>
<?php
 };//判断结束位置
?>

例如文章标题,在索引的时候显示成超链接,而在具体的文章页面,把标题显示为纯文本:

<?php
//判断开始
if ( is_single () || is_page()) : 
//如果是文章单页
?>
 
<h1><?php the_title();//只显示标题 ?></h1>
 
<?php else ://其他情况 ?>
 
<h2><a class="title" href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h2>
 
<?php  endif ?>
//判断结束位置
 
?>

当然应用还非常多,官网地址:https://codex.wordpress.org/Conditional_Tags


发布日期:

所属分类: WordPress 函数 标签:  


没有相关文章!