个性化阅读
专注于IT技术分析

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

我创建一个自定义帖子类型, 在主题的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


首先, 你必须创建CPT。在我们的示例中, CPT的名称为”项目”。

 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

之后, 刷新你的永久链接。

赞(0)
未经允许不得转载:srcmini » 如何为自定义帖子创建单个模板

评论 抢沙发

评论前必须登录!