在你所用的Wordpress主题(例如使用Wordpress自带的默认主题)文件夹下的文件functions.php的最后一个?>(如果有的话)之前,如果没有的话,直接添加如下代码:
- function sl_custom_post_product() {
- $labels = array(
- ‘name’ => _x( ‘产品’, ‘post type general name’ ),
- ‘singular_name’ => _x( ‘产品’, ‘post type singular name’ ),
- ‘add_new’ => _x( ‘添加’, ‘book’ ),
- ‘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’, ‘sl_custom_post_product’ );
即可在Wordpress后台左侧仪表盘看到:
上面代码中:
labels 是一个参数数组;
description的值是对这个自定义文章类型的描述;
public的值设置为true,后面将会介绍;
menu_position菜单在Wordpress后台左侧仪表盘中的位置:5意味着这个自定义文章类型在菜单中将会在“文章”菜单的下面,如果在functions.php中这段代码前面的代码或者包含文件中没有其它自定义文章类型的话,如果在上面这段代码之前已经有其它自定义文章类型,并且menu_position也是5的话,上面这段代码自定义的文章类型将会出现在那个自定义文章类型之后,例如我再上面这段代码之前,已经定义了一个名字叫幻灯的自定义文章类型,并且menu_position也是5,那么顺序是:
menu_position数值对应的位置:
- 5 -文章之下
- 10 – 多媒体之下
- 15 – 链接之下(链接在Wordpress3.5开始已经隐藏了)
- 20 – 页面之下
- 25 – 评论之下
- 60 – 第一个分隔符之下(一般是在外观的上面)
- 65 – 插件之下
- 70 -用户之下
- 75 – 工具之下
- 80 -设置之下
- 100 – 第二个分隔符之下(一般是在设置之下)
supports的值是一个数组,说明这个文章类型支持哪些特性:
- ‘title’
- ‘editor’ (内容编辑器)
- ‘author’
- ‘thumbnail’ (缩略图)
- ‘excerpt’
- ‘trackbacks’
- ‘custom-fields’
- ‘comments’ (是否支持评论)
- ‘revisions’ (是否自动备份)
- ‘page-attributes’ (menu order, hierarchical must be true to show Parent option)
- ‘post-formats’ add post formats
has_archive确定是否归档。
register_post_type()函数的参数说明,请参考:
http://codex.wordpress.org/Function_Reference/register_post_type
Leave a Reply