微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐