Skip to content

Instantly share code, notes, and snippets.

@robneu
Last active August 29, 2015 14:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robneu/11190903 to your computer and use it in GitHub Desktop.
Save robneu/11190903 to your computer and use it in GitHub Desktop.
Check if the current post is a particular post type.
<?php
/**
* Check the current post to see what type of post it is.
*
* This will return true whenever a post is equal to a specified post type.
* It can be useful when making global changes for a particular post type.
*
* @param $post_types array or string the type of posts you're wanting to check.
* @return bool true if the current post type matches any of the checked types.
* @author Robert Neu <http://wpbacon.com>
* @author Gary Jones <http://gamajo.com/>
*/
function prefix_is_cpt( $post_types ) {
if ( in_array( get_post_type(), (array) $post_types ) ) {
return true;
}
return false;
}
<?php
/**
* Do conditional stuff based on what post type the current post is.
*
* @uses prefix_is_cpt()
* @author Robert Neu <http://wpbacon.com>
*/
function prefix_do_stuff_for_post_types() {
if ( prefix_is_cpt( 'page' ) ) {
// Do something for pages.
}
$post_types = array(
'post',
'review',
'tutorial',
);
if ( prefix_is_cpt( $post_types ) ) {
// Do something else for these types.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment