wordpress - 在 WordPress 中为自定义帖子类型创建模板

标签 wordpress custom-post-type

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

如果我创建一个新模板,它只会在我添加页面时显示在管理中,但是当我添加新的帖子类型时,我无法选择特定模板。

问题已解决:

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

最佳答案

创建名为的页面:

single-{cpt-slug}.php 例如单品.php

它将在显示自定义帖子类型的页面时使用。即当有人去http://example.com/product/awesome-shoes/

关于wordpress - 在 WordPress 中为自定义帖子类型创建模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7243990/

相关文章:

php - 根据产品类别自定义购物车总计和结帐总计文本

php - Wordpress 访客登录重定向查看帖子

php - 按元数据显示 WordPress 自定义帖子

php - 自定义帖子类型搜索应该适用于前端而不是管理员

wordpress - WooCommerce PDF 发票和装箱单无法使用中文

javascript - 使用 jquery 更改字段

javascript - 称为 JSTree 的 JQuery 库用于制作嵌套的复选框

wordpress - 在自定义帖子中上传多张精选图片 (Wordpress)

php - 计算 ACF 中关系帖子的数量

php - 单击 Ajax WordPress 加载更多自定义帖子