Joshua David Nelson

Bullshit Free WordPress Development

  • Services
  • Code
  • About
  • Contact

Check If Post Type Supports Archives

Checks if a post type supports archives and returns boolean. Handy if you are playing with custom post types and page templates.

<?php
/**
* Check if post type supports an archive
*
* @param string $post_type post type name
* @uses get_post_type
* @global object $post
* @returns boolean
* @author Joshua David Nelson
*/
if( !function_exists( 'cpt_has_archive' ) ) {
function cpt_has_archive( $post_type ) {
if( !is_string( $post_type ) || !isset( $post_type ) )
return false;
// find custom post types with archvies
$args = array(
'has_archive' => true,
'_builtin' => false,
);
$output = 'names';
$archived_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( $archived_custom_post_types ) )
return false;
// check if post type is a supports archives
if ( in_array( $post_type, $archived_custom_post_types ) ) {
return true;
} else {
return false;
}
// if all else fails, return false
return false;
}
}
view raw cpt_has_archive.php hosted with ❤ by GitHub

Source: http://gist.github.com/joshuadavidnelson/8321291

Tags: archive, custom post type

Previous Code:
Is_Custom_Post_Type()
Back to the Code Snippets
Next Code:
Exclude Scripts from Scripts-to-Footer Plugin
  • 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