KillerFish
KillerFish

Reputation: 5170

Wordpress How to Check whether it is POST or PAGE

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

Upvotes: 50

Views: 78296

Answers (8)

Serhiy Zaharchenko
Serhiy Zaharchenko

Reputation: 1760

The is_singular() function is ideal for this case. You can check for posts or pages individually, like this:

if( is_singular('post') ) {
  // Do something for the post
} 

if( is_singular('page') ) {
  // Do something for the page
}

There is also a specific function for the pages:

if( is_page() ) {
  // Do something for the page
}

Alternatively, you can check for multiple post types in a single function call:

if( is_singular( ['post', 'page'] ) ) {
  // Do something for both post types
}

Upvotes: 1

20AMax
20AMax

Reputation: 466

You can also use get_post_type() function.

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

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

Upvotes: 24

Darshan Saroya
Darshan Saroya

Reputation: 54

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.

Upvotes: 1

jacr1614
jacr1614

Reputation: 1310

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

Upvotes: 0

Nate
Nate

Reputation: 1482

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

Upvotes: 9

quanghiep
quanghiep

Reputation: 27

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

Upvotes: 0

Jonas Lundman
Jonas Lundman

Reputation: 21

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

Upvotes: 1

joschi
joschi

Reputation: 13101

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

Upvotes: 51

Related Questions