如何为自定义帖子创建单个模板

时间:2018-03-06 17:08:22

标签: php wordpress wordpress-theming custom-post-type

我创建了一个自定义帖子类型,在主题functions.php

中添加以下代码
function  cptarchivePost_init() {
    $args = array(
      'label' => 'Archive Post',
        'public' => true,
        'show_ui' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'rewrite' => array('slug' => 'cpt_archive_post'),
        'query_var' => true,
        'menu_icon' => 'dashicons-video-alt',
        'supports' => array(
            'title',
            'editor',
            'excerpt',
            'trackbacks',
            'custom-fields',
            'comments',
            'revisions',
            'thumbnail',
            'author',
            'page-attributes',)
        );
    register_post_type( 'cpt_archive_post', $args );
}
add_action( 'init', 'cptarchivePost_init' );

add_action( 'init', 'create_cpt_archive_category', 0 );
function create_cpt_archive_category() {
    register_taxonomy(
        'cpt_archive_category',
        'cpt_archive_post',
        array(
            'labels' => array(
                'name' => 'Category',
                'add_new_item' => 'Add Category',
                'new_item_name' => "New Category"
            ),
            'show_ui' => true,
            'show_tagcloud' => false,
            'hierarchical' => true
        )
    );
}

并使用名称single-cpt_archive_post.php在主题中创建文件/模板,但仍使用index.php模板发布帖子

任何人都可以帮助我如何为自定义帖子

创建单个模板

1 个答案:

答案 0 :(得分:0)

首先,你必须创建你的CPT。在我们的示例中,CPT的名称是" projects"。

 function custom_taxonomies(){

        //Custom post type(Projects)
        $labels = array(
            'name' => _x('Projects', 'post type general name'),
            'singular_name' => _x('Project', 'post type singular name'),
            'add_new' => _x('Add new Project', 'member'),
            'add_new_item' => __('Add new Project'),
            'edit_item' => __('Edit'),
            'new_item' => __('New Project'),
            'view_item' => __('View'),
            'search_items' => __('Search Project'),
            'not_found' =>  __('No Projects found!'),
            'not_found_in_trash' => __('No Projects in the trash!'),
            'parent_item_colon' => ''
        );
        $args = array(
            'labels' => $labels,
            'public' => true,
            'publicly_queryable' => true,
            'show_ui' => true,
            'query_var' => true,
            'rewrite' => true,
            'capability_type' => 'post',
            'hierarchical' => false,
            'menu_position' => null,
            'taxonomies' => array('post_tag'),
            'supports' => array('title','thumbnail','editor','excerpt','author'),
            'menu_icon' => 'dashicons-feedback',
            'has_archive' => true
        );
        register_post_type('projects',$args);

        $labels = array(
            'name' => _x( 'Category', 'taxonomy general name' ),
            'singular_name' => _x( 'Category', 'taxonomy singular name' ),
            'search_items' =>  __( 'Search category' ),
            'all_items' => __( 'All categories' ),
            'parent_item' => __( 'Parent category' ),
            'parent_item_colon' => __( 'Parent category' ),
            'edit_item' => __( 'Edit category' ),
            'update_item' => __( 'Update category' ),
            'add_new_item' => __( 'Add new category' ),
            'new_item_name' => __( 'Category name' ),
        );

        register_taxonomy( 'projects_tax', array( 'projects' ), array(
            'hierarchical' => true,
            'labels' => $labels,
            'show_ui' => true,
            'query_var' => true,
            'rewrite' => array( 'slug' => 'projects_category' ),
        ));



    }

    add_action( 'init', 'custom_taxonomies', 0 );

然后你创建你的文件single-projects.php

之后刷新永久链接。