Last active
August 29, 2015 14:00
-
-
Save robneu/11190903 to your computer and use it in GitHub Desktop.
Check if the current post is a particular post type.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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