Wordpress How to Check whether it is POST or PAGE

后端 未结 7 2062
抹茶落季
抹茶落季 2020-12-30 19:50

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

相关标签:
7条回答
  • 2020-12-30 20:20

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

    0 讨论(0)
  • 2020-12-30 20:21

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

    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.

    Please advice.

    Thanks

    0 讨论(0)
  • 2020-12-30 20:23

    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().

    0 讨论(0)
  • 2020-12-30 20:25

    You can also use get_post_type() function.

    if (get_post_type() === 'post') {
        // POST
    }
    
    if (get_post_type() === 'page') {
        // PAGE
    }
    
    0 讨论(0)
  • 2020-12-30 20:29

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

    0 讨论(0)
  • 2020-12-30 20:29

    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 $post object (of type WP_Post) and examine the $post->post_type property. Possible values include 'post' and 'page'.

    0 讨论(0)
提交回复
热议问题