WordPress How to Check Whether it is POST or PAGE?

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

Solution 1

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

Solution 2 (Trustworthy)

is_single() will also return true for custom post types, if you want to be 100% sure it’s a post then use is_singular(‘post’)

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

Solution 3

You can also use get_post_type() function.

if (get_post_type() === ‘post’) {
// POST
}

if (get_post_type() === ‘page’) {
// PAGE
}

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’.

is_single()

Related Posts