JamesHusband
5/25/2017 - 9:15 AM

gistfile1.txt

  public function post_type_choose_templates() {
		global $post;
		$theme = wp_get_theme();
		$post_types_templates_directory = $theme->get_stylesheet_directory() . '/landing-page-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[ 'landing-page-templates/' . $post->post_type . '/' . $file ] = _cleanup_header_comment( $header[1] );
    		}
        
        
    		// Build the Template dropdown ?>
    		<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.";
        }
		
	}