Skip to content

Instantly share code, notes, and snippets.

@michaelbragg
Created February 24, 2016 14:21
Show Gist options
  • Select an option

  • Save michaelbragg/376df8e245d6989db2b7 to your computer and use it in GitHub Desktop.

Select an option

Save michaelbragg/376df8e245d6989db2b7 to your computer and use it in GitHub Desktop.
WordPress: Checks if paramater(s) are in the post type
<?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;
<?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