Skip to content

Instantly share code, notes, and snippets.

@flashingcursor
Created August 4, 2012 20:02
Show Gist options
  • Save flashingcursor/3259625 to your computer and use it in GitHub Desktop.
Save flashingcursor/3259625 to your computer and use it in GitHub Desktop.
Creating a default template for WordPress custom post types with Theme override. This is broken down to limit it to a single view, but could also be modified for a default archive view, etc...
<?php
add_filter( 'template_include', 'default_mycpt_template', 100 );
/**
* Setup a default template for your CPT if the current theme doesn't already have one.
* @return void
*/
function default_mycpt_template( $template )
{
$post_type = 'my_custom_post_type';
// Check if the current theme has a template for our post type.
if ( FALSE !== strpos( $template, "/single-$post_type.php" )
{
return $template;
}
// Send our plugins version of the post type layout
if ( is_singular() && $post_type === get_post_type( $GLOBALS['post'] )
{
return dirname( __FILE__ ) . "/single-$post_type.php";
}
// Not our post type single view.
return $template;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment