在Wordpress中为自定义帖子类型创建模板

在Wordpress中为自定义帖子类型创建模板,wordpress,custom-post-type,Wordpress,Custom Post Type,我知道如何为特定页面创建自定义模板。但是,我想为特定的自定义帖子类型创建一个模板。这可能吗?如果是真的,我该怎么做 如果我创建了一个新模板,它将仅在我添加页面时显示在管理中,但当我添加一个新的帖子类型时,我没有选择某个模板的选项 问题已解决: /* Show the list of available custom templates templates in the Custom Post Type admin section */ /** * Post_type */ define(

我知道如何为特定页面创建自定义模板。但是,我想为特定的自定义帖子类型创建一个模板。这可能吗?如果是真的,我该怎么做

如果我创建了一个新模板,它将仅在我添加页面时显示在管理中,但当我添加一个新的帖子类型时,我没有选择某个模板的选项

问题已解决:

/* 
Show the list of available custom templates templates in the Custom Post Type admin section
*/

/**
 * Post_type
 */
define( 'MY_THEME_POST_TYPE', 'cases' );
/**
 * Load the page template for any post object
 * having the appropriate meta key set.
 */
add_action( 'template_redirect', 'mytheme_template_redirect' );
function mytheme_template_redirect() {
    global $wp_query;
    $id = (int) $wp_query->get_queried_object_id();
    $template = get_post_meta( $id, '_wp_page_template', true );
    if ( $template && 'default' !== $template ) {
        $file = STYLESHEETPATH . '/' . $template;
        if( is_file( $file ) ) {
            require_once $file;
            exit;
        }
    }

}
/**
 * Process the Meta Box
 * @todo Permissions check.
 * @todo Filter input.
 * @todo Nonces.
 */
add_action( 'save_post', 'mytheme_process_resource_template' );
function mytheme_process_resource_template() {
    global $post;

    /* Sanitize $_POST array. */
    $clean_id = ( isset( $_POST['ID'] ) ) ? intval( $_POST['ID'] ) : 0;

    if ( !empty( $_POST['page_template'] ) && MY_THEME_POST_TYPE == $post->post_type ) {
        $page_templates = get_page_templates();
        if ( 'default' != $page_template && !in_array( $_POST['page_template'], $page_templates ) ) {
            if ( $wp_error )
                return new WP_Error('invalid_page_template', __('The page template is invalid.'));
            else
                return 0;
        }
        update_post_meta( $clean_id, '_wp_page_template',  $_POST['page_template'] );
    }
}
/**
 * Registers the Meta Box
 * @uses mytheme_page_attributes_meta_box()
 */
add_action( 'admin_init', 'mytheme_register_meta_boxes', 10 );
function mytheme_register_meta_boxes()  {
    add_meta_box(
        'mytheme_post_type_template',
        'Template',
        'mytheme_page_attributes_meta_box',
        MY_THEME_POST_TYPE,
        'side',
        'low'
        );
}
/**
 * Creates the Meta Box
 */
function mytheme_page_attributes_meta_box() {
    global $post;
    $post_type_object = get_post_type_object($post->post_type);    
    if ( 0 != count( get_page_templates() ) ) {
        $template = get_post_meta( $post->ID, '_wp_page_template',  true );
        ?>
<p><strong><?php _e('Template') ?></strong></p>
<label class="screen-reader-text" for="page_template"><?php _e('Page Template') ?></label><select name="page_template" id="page_template">
<option value='default'><?php _e('Default Template'); ?></option>
<?php page_template_dropdown( $template ); ?>
</select>
<?php
    }
}
/*
在“自定义帖子类型管理”部分中显示可用自定义模板的列表
*/
/**
*后置式
*/
定义('MY_THEME_POST_TYPE','cases');
/**
*加载任何post对象的页面模板
*设置了适当的元键。
*/
添加操作('template_redirect'、'myteme_template_redirect');
函数mytheme_模板_重定向(){
全局$wp_查询;
$id=(int)$wp\u query->get\u queryed\u object\u id();
$template=get_post_meta($id,'u wp_page_template',true);
如果($template&‘default’!==$template){
$file=STYLESHEETPATH.'/'.$template;
if(is_文件($file)){
需要_once$文件;
出口
}
}
}
/**
*处理元框
*@todo权限检查。
*@todo过滤器输入。
*@todo Nonces。
*/
添加操作('save_post'、'myteme_process_resource_template');
函数mytheme\u流程\u资源\u模板(){
全球$员额;
/*清理$\u POST数组*/
$clean_id=(isset($_POST['id'])?intval($_POST['id']):0;
如果(!empty($\u POST['page\u template'])和&MY\u THEME\u POST\u TYPE==$POST->POST\u TYPE){
$page_templates=获取页面模板();
if('default'!=$page\u template&&!在数组中($\u POST['page\u template'],$page\u templates)){
如果($wp_错误)
返回新的WP_错误('invalid_page_template',uuu('page template is invalid');
其他的
返回0;
}
更新帖子元($clean_id,''wp_page_template',$帖子['page_template']);
}
}
/**
*注册元框
*@使用mytheme_页面_属性_元_框()
*/
添加操作('admin_init'、'myteme_register_meta_box',10);
函数mytheme\u寄存器\u元框(){
添加元框(
“mytheme\u post\u type\u模板”,
“模板”,
“mytheme\u页面\u属性\u元框”,
我的主题帖子类型,
"一边",,
“低”
);
}
/**
*创建元框
*/
函数mytheme\u页面\属性\元\框(){
全球$员额;
$post\u type\u object=get\u post\u type\u object($post->post\u type);
如果(0!=计数(获取页面模板()){
$template=get_post_meta($post->ID,'wp_page_template',true);
?>


创建名为:

single-{cpt slug}.php,例如single-product.php


它将在显示自定义帖子类型的页面时使用。例如,当有人进入

我必须将此模板文件放在哪个文件夹时?主题根目录。如果您的主题是
awesome主题
,则比
wordpress/wp content/themes/awesome主题/single product.php更有效。我还没有测试,但有一些插件可以为您启用模板自定义post_类型: