Joshua David Nelson

Bullshit Free WordPress Development

  • Services
  • Code
  • About
  • Contact

Is_Custom_Post_Type()

Checks if the current post is a custom post type, returns boolean. Very handy when working with custom post types, layouts and the like.

<?php
/**
* Checks if current post is a custom post type, returns boolean
*
* @author Joshua David Nelson
* @link http://joshuadnelson.com/code/is_custom_post_type/
*/
if ( !function_exists( 'is_custom_post_type' ) ) {
function is_custom_post_type() {
// find custom post types
$args = array(
//'public' => true, // -- uncomment if you only want "public" cpts
'_builtin' => false,
);
$output = 'names';
$public_custom_post_types = get_post_types( $args, $output );
// if there are no custom post types, then the current post can't be one
if( empty( $public_custom_post_types ) )
return false;
// access global variable
global $post;
// get the current post type, returns a string or false on failure
$post_type = get_post_type( $post );
// check if current post type is a custom post type
if ( $post_type && in_array( $post_type, $public_custom_post_types ) ) {
return true;
} else {
return false;
}
// if all else fails, return false
return false;
}
}
view raw is_custom_post_type.php hosted with ❤ by GitHub

Source: https://gist.github.com/joshuadavidnelson/8291503

Tags: conditional, custom post type, page type

Previous Code:
Alternate Sidebar Page Template in Genesis
Back to the Code Snippets
Next Code:
Check If Post Type Supports Archives
  • Twitter
  • RSS Feed URL

About Me

I'm a WordPress Engineer. I build sleek, custom websites with WordPress and Genesis.

See my services and my recent work.

Contact me to get your project started.

Gravity Forms Plugin for WordPress Fastest WordPress Hosting

Recent Posts

  • Using Font Awesome Icons for WooCommerce Grid / List Toggle
  • Disable Blog: WordPress Gone Blog-less
  • Category (Taxonomy) Dropdown Filtered By Post Type
  • Weather in WordPress with Dark Sky
  • Fixing Your Deprecated Widget Constructors in WordPress 4.3
  • Twitter
  • RSS Feed URL
  • Code Snippets
  • My Plugins
  • Make a Payment

© Joshua David Nelson | Hand-Forged | WordPress + Genesis | Terms of Service | Legal | Contact