Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JamesHusband/7f74b26a7b45ac0e64e0347108e9f67a to your computer and use it in GitHub Desktop.
Save JamesHusband/7f74b26a7b45ac0e64e0347108e9f67a to your computer and use it in GitHub Desktop.
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.";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment