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