Wordpress 如何检查是 POST 还是 PAGE

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4290420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:41:13  来源:igfitidea点击:

Wordpress How to Check whether it is POST or PAGE

wordpress

提问by KillerFish

How to check if an article is a post or a page in WordPress?

如何检查文章是 WordPress 中的帖子还是页面?

回答by joschi

You can use the is_page()and is_single()functions.

您可以使用is_page()is_single()函数。

回答by Nate

If you're looping through a collection of posts/pages (say, on a search results page), then is_single()and is_page()won't be of any use. In this situation, you could grab the global $postobject (of type WP_Post) and examine the $post->post_typeproperty. Possible values include 'post' and 'page'.

如果您正在遍历一系列帖子/页面(例如,在搜索结果页面上),那么is_single()并且is_page()将没有任何用处。在这种情况下,您可以获取全局$post对象(类型为WP_Post)并检查$post->post_type属性。可能的值包括“post”和“page”。

回答by 20AMax

You can also use get_post_type()function.

您还可以使用get_post_type()函数。

if (get_post_type() === 'post') {
    // POST
}

if (get_post_type() === 'page') {
    // PAGE
}

回答by Jonas Lundman

is_singular() returns true for a single post, page or attachment

is_singular() 为单个帖子、页面或附件返回 true

回答by jacr1614

if you want y?to know the page that list the posts , and you are using the posts page option in the configuration, You should use is_home().

如果您想知道列出帖子的页面,并且您在配置中使用了帖子页面选项,则应该使用is_home().

回答by Darshan Saroya

It's for developer, if you are not a developer you can also check current page type. You have to just inspect particular page and see body tag. If theme is build with basic WordPress rules then body tag have classes related to page or single page. These classes may b included the post type, template name, file name, page id and many more.

这是给开发人员的,如果您不是开发人员,还可以查看当前页面类型。您只需检查特定页面并查看正文标签。如果主题是使用基本的 WordPress 规则构建的,那么正文标签具有与页面或单页相关的类。这些类可能包括帖子类型、模板名称、文件名、页面 ID 等等。

回答by quanghiep

You mean that is_single()will return true if it is a post ? (not a page), am I right,

你的意思是,is_single()如果它是一个帖子,它会返回 true 吗?(不是一页),我说得对吗,

I like that, I think you wrong, because I have a plugin show some text on only post, I'm using is_single()but It also show on pages.

我喜欢这样,我认为你错了,因为我有一个插件只在帖子上显示一些文本,我正在使用is_single()但它也在页面上显示。

Please advice.

请指教。

Thanks

谢谢