Created
May 19, 2016 20:32
-
-
Save ronalfy/2d957a861237d6207b14d2f7106b1327 to your computer and use it in GitHub Desktop.
Custom Post Type Template post_type_choose_templates
This file contains hidden or 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 | |
public function post_type_choose_templates() { | |
global $post; | |
$theme = wp_get_theme(); | |
$post_types_templates_directory = $theme->get_stylesheet_directory() . '/cpt-templates/' . $post->post_type; | |
$files = self::scandir( $post_types_templates_directory, array( 'php' ) ); | |
$current_page_template = get_post_meta( $post->ID, '_wp_page_template', true ); | |
if ( ! $current_page_template ) { | |
$current_page_template = 'none'; | |
} | |
$page_templates = array(); | |
if ( $files ) { | |
foreach ( $files as $file => $full_path ) { | |
if ( ! preg_match( '|Template Name:(.*)$|mi', file_get_contents( $full_path ), $header ) ) | |
continue; | |
$page_templates[ 'cpt-templates/' . $post->post_type . '/' . $file ] = _cleanup_header_comment( $header[1] ); | |
} | |
?> | |
<select name="post_type_page_template"> | |
<option value="none" <?php selected( 'none', $current_page_template ); ?>>None</option> | |
<?php | |
foreach( $page_templates as $file => $name ) { | |
printf( '<option value="%s" %s>%s</option>', esc_attr( $file ), selected( $file, $current_page_template, false ), esc_attr( $name ) ); | |
} | |
?> | |
</select> | |
<?php | |
} else { | |
echo "There are no page templates for this post type."; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment