Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

Well, even at a quick glance, I can see that you are using the same meta box ID - postparentdiv - both times, which probably causes issues internally.

That said, this seems a convoluted path to take at all, and possibly an XY Problem.

Why do you need to scan for templates at all?

share|improve this answer
    
Im creating 3 post types that need to have multiple templates for selection. @vancoder –  mygm26 Sep 24 '13 at 1:03
    
I changed one of the postparentdiv's name but it didnt change nothing. As far it being xy problem, the above code works perfect for one post type so now I am trying to use it for multiple post types, this way each post type has a template name to look for. –  mygm26 Sep 24 '13 at 1:07
up vote 0 down vote accepted

OK I was able to get it figured out. Im going to post my code incase anyone else will find it useful. Basically what it does is add a dropdown to the edit post screen that allows you to choose page templates for custom post types. --

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', 'high');
}
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;
}

// Create the meta box
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="pa_post_template"><?php _e('Practice Area Template') ?></label><select name="pa_post_template" id="pa_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('postparentdiv2', __('Practice Area Template'), 'pract_template_meta_box', 'practice-area', 'side', 'core');
}

// Get template files 
function get_post_templates2() {
  $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( '|Practice Template:(.*)$|mi', $template_data, $name ) )
          $name = _cleanup_header_comment($name[1]);

        if ( !empty( $name ) ) {
          $post_templates[trim( $name )] = $basename;
        }
      }
    }
  }

  return $post_templates;
}

// Create the Dropdown
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;
}

// Save the selection
add_action('save_post','save_pa_post_template',10,2);
function save_pa_post_template($post_id,$post) {
  if ($post->post_type=='practice-area' && !empty($_POST['pa_post_template']))
    update_post_meta($post->ID,'_practa_template',$_POST['pa_post_template']);
}

// Call the template chosen
add_filter('single_template','get_post_template_for_template2_loader');
function get_post_template_for_template2_loader($template) {
  global $wp_query;
  $post = $wp_query->get_queried_object();
  if ($post) {
    $post_template = get_post_meta($post->ID,'_practa_template',true);
    if (!empty($post_template) && $post_template!='default')
      $template = get_stylesheet_directory() . "/{$post_template}";
  }
  return $template;
}

// Create the CPT 3 page templates meta box
function about_template_meta_box($post) {
  if ( 'about' == $post->post_type && 0 != count( get_post_templates3() ) ) {
    $template = get_post_meta($post->ID,'_bout_template',true);
    ?>
<label class="screen-reader-text" for="ab_post_template"><?php _e('About Template') ?></label><select name="ab_post_template" id="ab_post_template">
<option value='default'><?php _e('Default Template'); ?></option>
<?php post_template_dropdown3($template); ?>
</select>
<?php
  } ?>
<?php
}
add_action('add_meta_boxes','add_about_template_metabox');

function add_about_template_metabox() {
    add_meta_box('postparentdiv3', __('About Template'), 'about_template_meta_box', 'about', 'side', 'core');
}

// Get template files 
function get_post_templates3() {
  $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( '|About Page Template:(.*)$|mi', $template_data, $name ) )
          $name = _cleanup_header_comment($name[1]);

        if ( !empty( $name ) ) {
          $post_templates[trim( $name )] = $basename;
        }
      }
    }
  }

  return $post_templates;
}

// Create the Dropdown
function post_template_dropdown3( $default = '' ) {
  $templates = get_post_templates3();
  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;
}

// Save the selection
add_action('save_post','save_about_post_template',10,2);
function save_about_post_template($post_id,$post) {
  if ($post->post_type=='about' && !empty($_POST['ab_post_template']))
    update_post_meta($post->ID,'_bout_template',$_POST['ab_post_template']);
}

// Call the template chosen
add_filter('single_template','get_post_template_for_template3_loader');
function get_post_template_for_template3_loader($template) {
  global $wp_query;
  $post = $wp_query->get_queried_object();
  if ($post) {
    $post_template = get_post_meta($post->ID,'_bout_template',true);
    if (!empty($post_template) && $post_template!='default')
      $template = get_stylesheet_directory() . "/{$post_template}";
  }
  return $template;
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.