十一年专注,只做WordPress定制开发一件事

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

王超
2017-09-11
主题开发
9,220 次

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

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', $args );
}
add_action( 'init', 'my_taxonomies_product', 0 );

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

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

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

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

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

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

文章标签:

WordPress日记主要承接WordPress主题定制开发PSD转WordPressWordPress仿站以及以WordPress为管理后端的小程序、APP,我们一直秉持“做一个项目,交一个朋友”的理念,希望您是我们下一个朋友。如果您有WordPress主题开发需求,可随时联系QQ:919985494 微信:18539976310

搜索

嘿,有问题找我来帮您!