明凯博客

关注网站技术,一个特立独行的程序员

WordPress中is_single()与is_singular()的区别详解

简单的认为is_sigular()相当于is_single()||is_page()||is_attachment()没有什么大问题。
也就是说,有is_single()||is_page()

1
< ?php if ( is_single()||is_page() ) { ?>

的地方,都可以替换成is_singular()

1
< ?php if ( is_singular() ) { ?>

但是其实上述这种理解不是太准确。来比较一下is_single()与is_sigular()的文档。

is_single()是可以加参数的。参数可以是post_id,post_slug,post_title。2.5之后还可以同时对这几个参数加以判断。
当不带参数时,返回的是当前显示的是不是一个单独的post(而且类型不是page和attachment)。
而带了参数,返回的就是当前显示的是不是编号/标题/标志为“XXX”的那篇post。
在数据库里,is_single不仅要查询ID是否存在,还要保证type是“post”而不是“page”,“attachment”甚至“revision”。

is_sigular()则简单的多。就是当前显示的是不是一条单独的post所形成的页面。
在数据库里,is_sigular()也只是判断ID是否存在即可。

, , ,

相关文章

2 条评论 “WordPress中is_single()与is_singular()的区别详解

  • FISH CHEAP说道:

    Hello there, I found your blog by means of Google while looking for a related subject, your
    website got here up, it appears to be like great. I’ve bookmarked
    it in my google bookmarks.
    Hello there, just was aware of your weblog via Google, and located that it is really
    informative. I am going to watch out for brussels. I will
    appreciate should you proceed this in future. Many people will likely
    be benefited from your writing. Cheers!

  • Sign Up说道:

    Thank you for your sharing. I am worried that I lack creative ideas. It is your article that makes me full of hope. Thank you. But, I have a question, can you help me?

发表回复

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