Created
February 24, 2016 14:21
-
-
Save michaelbragg/376df8e245d6989db2b7 to your computer and use it in GitHub Desktop.
WordPress: Checks if paramater(s) are in the 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 | |
| /** | |
| * Example One: String | |
| */ | |
| if( is_post_type( 'post_type01' ) ): | |
| echo "This is the post type"; | |
| endif; | |
| /** | |
| * Example Two: Array | |
| */ | |
| if( is_post_type( array( 'post_type01', 'post_type02', 'post_type03', 'post_type04', 'post_type05' ) ) ): | |
| echo "This is one of the post types supplied"; | |
| endif; |
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 | |
| // ... | |
| /** | |
| * Checks if paramater(s) are in the post type | |
| * | |
| * @param int/string/array $post_types Post type name(s) or id | |
| * @return boolean if in post type | |
| * @version 1.0.0 | |
| * @since 1.0.0 | |
| */ | |
| if ( ! function_exists( 'is_post_type' ) ) : | |
| function is_post_type( $post_types = null ) { | |
| // If our variable is not set stop as soon as possible | |
| if( ! isset( $post_types ) ) { | |
| return false; | |
| } | |
| $array = array(); | |
| if( $post_types == get_post_type() ){ | |
| array_push( $array, $post_type ); | |
| return true; | |
| } | |
| foreach ( $post_types as $value ) { | |
| if( $value == get_post_type() ){ | |
| array_push( $array, $value ); | |
| return true; | |
| } | |
| } | |
| return false; | |
| } | |
| endif; | |
| // ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment