WordPress创建自定义文章类型分类模板和文章页模板

有时候我们需要创建一个自定义文章类型来让网站管理更加方便,而且创建后我们需要针对自定义文章类型制作对应的分类页模板和文章页模板,比如我们创建了下面这个自定义文章类型

function my_custom_post_product() {

$labels = array(

'name' => _x( '产品','post type 名称' ),

'singular_name' => _x( '产品','post type 单个 item 时的名称,因为英文有复数' ),

'add_new' => _x( '新建产品','添加内容链接名称' ),

'add_new_item' => __( '新建一个产品' ),

'edit_item' => __( '编辑产品' ),

'new_item' => __( '新产品' ),

'all_items' => __( '所有产品' ),

'view_item' => __( '查看产品' ),

'search_items' => __( '搜索产品' ),

'not_found' => __( '没有找到有关产品' ),

'not_found_in_trash' => __( '回收站里面没有相关产品' ),

'parent_item_colon' => '',

'menu_name' => '产品中心'

);

$args = array(

'labels' => $labels,

'description' => '我们网站的产品信息',

'public' => true,

'menu_position' => 5,

'supports' => array( 'title','editor','thumbnail','excerpt','comments' ),

'has_archive' => true

);

register_post_type( 'product',$args );

}

add_action( 'init','my_custom_post_product' );

function my_taxonomies_product() {

$labels = array(

'name' => _x( '产品分类','taxonomy 名称' ),

'singular_name' => _x( '产品分类','taxonomy 单数名称' ),

'search_items' => __( '搜索产品分类' ),

'all_items' => __( '所有产品分类' ),

'parent_item' => __( '该产品分类的上级分类' ),

'parent_item_colon' => __( '该产品分类的上级分类:' ),

'edit_item' => __( '编辑产品分类' ),

'update_item' => __( '更新产品分类' ),

'add_new_item' => __( '添加新的产品分类' ),

'new_item_name' => __( '新产品分类' ),

'menu_name' => __( '产品分类' ),

);

$args = array(

'labels' => $labels,

'hierarchical' => true,

);

register_taxonomy( 'product_category','product','my_taxonomies_product',0 );

注意一下这连个地方:(‘product’和‘product_category’)

register_post_type( 'product',$args ); /**创建自定义文章类型的名字**/

register_taxonomy( 'product_category',$args );/**创建自定义文章分类的名字**/

1、创建自定义文章类型分类页模板

只需要将您的分类模板命名为taxonomy-product_category.PHP即可,页就是命名为taxonomy-自定义文章类型分类的名字.PHP

2,创建自定义文章类型文章页模板

只需要将您的文章页模板命名为single-product.PHP即可,页就是命名为single-自定义文章类型的名字.PHP

相关文章

我们有时候在定制WORDPRESS主题的时候,由于菜单样式的要求我...
很多朋友在做wordpree主题制作的时候会经常遇到一个问题,那...
wordpress后台的模块很多,但并不是每个都经常用到。介绍几段...
从WordPress4.2版本开始,如果我们在MYSQL5.1版本数据中导出...
很多网友会遇到这样一个问题,就是WordPress网站上传图片、附...
对于经常要在文章中出现代码的IT相关博客,安装一个代码高亮...