I am using the following code to create a dropdown on the edit post screen which will allow users to choose page templates for the post type. The code is working but when I try using it multiple times (for each post type) it no longer works. I am trying to have a dropdown for 3 post types, and each post type will have their own selections. Heres my code for the one post type ---
function attorney_template_meta_box($post) {
if ( 'attorney' == $post->post_type && 0 != count( get_post_templates() ) ) {
$template = get_post_meta($post->ID,'_attorney_template',true);
?>
<label class="screen-reader-text" for="att_post_template"><?php _e('Attorney Template') ?></label><select name="att_post_template" id="att_post_template">
<option value='default'><?php _e('Default Template'); ?></option>
<?php post_template_dropdown($template); ?>
</select>
<?php
} ?>
<?php
}
add_action('add_meta_boxes','add_attorney_template_metabox');
function add_attorney_template_metabox() {
add_meta_box('postparentdiv', __('Attorney Template'), 'attorney_template_meta_box', 'attorney', 'side', 'core');
}
function get_post_templates() {
$themes = get_themes();
$theme = get_current_theme();
$templates = $themes[$theme]['Template Files'];
$post_templates = array();
if ( is_array( $templates ) ) {
$base = array( trailingslashit(get_template_directory()), trailingslashit(get_stylesheet_directory()) );
foreach ( $templates as $template ) {
$basename = str_replace($base, '', $template);
if ($basename != 'functions.php') {
// don't allow template files in subdirectories
if ( false !== strpos($basename, '/') )
continue;
$template_data = implode( '', file( $template ));
$name = '';
if ( preg_match( '|Attorney Template:(.*)$|mi', $template_data, $name ) )
$name = _cleanup_header_comment($name[1]);
if ( !empty( $name ) ) {
$post_templates[trim( $name )] = $basename;
}
}
}
}
return $post_templates;
}
function post_template_dropdown( $default = '' ) {
$templates = get_post_templates();
ksort( $templates );
foreach (array_keys( $templates ) as $template )
: if ( $default == $templates[$template] )
$selected = " selected='selected'";
else
$selected = '';
echo "\n\t<option value='".$templates[$template]."' $selected>$template</option>";
endforeach;
}
add_action('save_post','save_att_post_template',10,2);
function save_att_post_template($post_id,$post) {
if ($post->post_type=='attorney' && !empty($_POST['att_post_template']))
update_post_meta($post->ID,'_attorney_template',$_POST['att_post_template']);
}
add_filter('single_template','get_post_template_for_template_loader');
function get_post_template_for_template_loader($template) {
global $wp_query;
$post = $wp_query->get_queried_object();
if ($post) {
$post_template = get_post_meta($post->ID,'_attorney_template',true);
if (!empty($post_template) && $post_template!='default')
$template = get_stylesheet_directory() . "/{$post_template}";
}
return $template;
}
by itself its fine, but when I try to add another one, it doesnt work. The metabox shows up but the drop down doesnt and the original post type's template doesnt even take into effect. Here's the additional code
// Practice Area
function pract_template_meta_box($post) {
if ( 'practice-area' == $post->post_type && 0 != count( get_post_templates2() ) ) {
$template = get_post_meta($post->ID,'_practa_template',true);
?>
<label class="screen-reader-text" for="pta_post_template"><?php _e('Practice Areas Template') ?></label><select name="pract_post_template" id="pract_post_template">
<option value='default'><?php _e('Default Template'); ?></option>
<?php post_template_dropdown2($template); ?>
</select>
<?php
} ?>
<?php
}
add_action('add_meta_boxes','add_pract_template_metabox');
function add_pract_template_metabox() {
add_meta_box('postparentdiv', __('Practice Area Template'), 'pta_template_meta_box', 'practice-area', 'side', 'core');
}
function get_post_templates2() {
$themes = get_themes();
$theme = get_current_theme();
$templates = $themes[$theme]['Template Files'];
$post_templates2 = array();
if ( is_array( $templates ) ) {
$base = array( trailingslashit(get_template_directory()), trailingslashit(get_stylesheet_directory()) );
foreach ( $templates as $template ) {
$basename = str_replace($base, '', $template);
if ($basename != 'functions.php') {
// don't allow template files in subdirectories
if ( false !== strpos($basename, '/') )
continue;
$template_data = implode( '', file( $template ));
$name = '';
if ( preg_match( '|Practice Area Template:(.*)$|mi', $template_data, $name ) )
$name = _cleanup_header_comment($name[1]);
if ( !empty( $name ) ) {
$post_templates2[trim( $name )] = $basename;
}
}
}
}
return $post_templates2;
}
function post_template_dropdown2( $default = '' ) {
$templates = get_post_templates2();
ksort( $templates );
foreach (array_keys( $templates ) as $template )
: if ( $default == $templates[$template] )
$selected = " selected='selected'";
else
$selected = '';
echo "\n\t<option value='".$templates[$template]."' $selected>$template</option>";
endforeach;
}
add_action('save_post','save_pta_post_template',10,2);
function save_pta_post_template($post_id,$post) {
if ($post->post_type=='practice-area' && !empty($_POST['pta_post_template']))
update_post_meta($post->ID,'_practa_template',$_POST['pta_post_template']);
}
add_filter('single_template','get_post_template_for_template2_loader');
function get_post_template_for_template_loader2($template) {
global $wp_query;
$post = $wp_query->get_queried_object();
if ($post) {
$post_template2 = get_post_meta($post->ID,'_practa_template',true);
if (!empty($post_template2) && $post_template2!='default')
$template = get_stylesheet_directory() . "/{$post_template2}";
}
return $template;
}
EDIT
The first code snippet works by itself, but the second code snippet doesnt work by itself.