WordPress开发:is_singular()函数,判断当前页面是否是指定文章类型

2018-10-1910:37:45网站建设与开发Comments2,182 views字数 546阅读模式

wordpress主题或插件开发过程中经常会用到页面判断函数,如判断当前页面是否是文章内容页的函数is_single()、判断是否是单页面的函数is_page(),如果是判断当前页面是文章或单页面时就要结合两个函数一起使用。而is_singular()则是这两个函数的升级版,通过这一个函数即可以实现前面所述的判断,并且支持附件、自定义文章类型的判断。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/cms/6830.html

函数代码:

1
is_singular( string|array $post_types = '' )
参数说明:

$post_types – 字符串或数组(string|array),文章类型的一个数组,可以是page、post、attachment或custom post types,默认值为空文章源自菜鸟学院-https://www.cainiaoxueyuan.com/cms/6830.html

返回值:

如果结果是要判断的页面则返回true,否则返回false文章源自菜鸟学院-https://www.cainiaoxueyuan.com/cms/6830.html

示例:

判断当前页面是否是文章内容页文章源自菜鸟学院-https://www.cainiaoxueyuan.com/cms/6830.html

1
2
3
4
5
<?php 
if(is_singular('post')){
	echo '这是文章post页面';
}
?>

判断当前页面是否是文章内容页或单页面文章源自菜鸟学院-https://www.cainiaoxueyuan.com/cms/6830.html

1
2
3
4
5
<?php 
if(is_singular(array('post','page')){
	echo '这是文章内容页或单页面';
}
?>

函数位置

is_singular()函数位于:wp-includes/query.php文章源自菜鸟学院-https://www.cainiaoxueyuan.com/cms/6830.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/cms/6830.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定