Created
December 15, 2016 17:02
-
-
Save richjenks/070a04aabe2e3d263ea4d28b3e14c08c to your computer and use it in GitHub Desktop.
Specify a template for a post type while storing the template file outside of a theme
This file contains 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 | |
/** | |
* Allows you to specify a template for a post type while storing the | |
* template file outside of a theme. | |
* | |
* @param string $file Full path to template file | |
* @param string $post_type Post Type to use template | |
*/ | |
function set_single_template( $file, $post_type ) { | |
add_filter( 'single_template', function( $single ) use ( $file, $post_type ) { | |
if ( $GLOBALS['post']->post_type === $post_type ) { | |
if ( file_exists( $file ) ) { | |
return $file; | |
} | |
} | |
return $single; | |
} ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment