自定义帖子类型的页面模板下拉列表

时间:2013-09-23 作者:730wavy

我使用以下代码在编辑帖子屏幕上创建一个下拉列表,允许用户为帖子类型选择页面模板。代码正在工作,但当我尝试多次使用它(针对每种帖子类型)时,它就不再工作了。我试图有一个下拉列表为3个职位类型,每个职位类型将有自己的选择。这是我的单篇文章类型代码---

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;
}
它本身很好,但当我尝试添加另一个时,它不起作用。metabox出现了,但下拉列表没有显示,原始帖子类型的模板甚至没有生效。这是附加代码

// 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

第一个代码段可以自己工作,但第二个代码段不能自己工作。

2 个回复
最合适的回答,由SO网友:730wavy 整理而成

好的,我能弄明白。我将发布我的代码,以防其他人发现它有用。基本上,它所做的是在编辑帖子屏幕上添加一个下拉列表,允许您为自定义帖子类型选择页面模板

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;
}

SO网友:vancoder

好吧,即使快速看一眼,我也可以看出您两次都在使用相同的元框ID-postparentdiv-这可能会导致内部问题。

也就是说,这似乎是一条曲折的道路,可能XY Problem.

为什么需要扫描模板?

结束