> WordPress中文手册 > wordpress进阶教程(二):注册一个自定义的文章类型

为什么要给WordPress注册默认文章之外的自定义的文章类型?比如本站,“教程”是按一般的博客文章形式来显示的,但是还有个“主题类”的文章,得用另一种显示形式,这样用普通的方法恐怕很难实现,所以需要另外注册一种文章类型,wp对每种文章类型可以用单独的模板来显示,这样就可以很方便实现我们要的效果了。

不过自定义文章类型的实际用途不仅仅局限于此,比如我就看到过有专门注册一个自定义文章类型只是用来给主题首页一个图片切换banner添加图片等设置信息的。。

看了前一篇文章,我想创建一个自定义文章类型(custom post type)使用的函数已经很明了了。

<?php    register_post_type( $post_type, $args );    //$post_type-必需,文章类型的名称    //args-可选,一个配置数组    ?>   include_once('post-type.php');   <?php    add_action('init', 'my_custom_init');    function my_custom_init()     {      $labels = array(        'name' => '书本name',        'singular_name' => '书本singularname',        'add_new' => 'Add_new',        'add_new_item' => 'add_new_item',        'edit_item' => 'edit_item',        'new_item' => 'new_item',        'view_item' => 'view_item',        'search_items' => 'search_items',        'not_found' =>  'not_found',        'not_found_in_trash' => 'not_found_in_trash',         'parent_item_colon' => '',        'menu_name' => 'menu_name'         );      $args = array(        'labels' => $labels,        'public' => true,        'publicly_queryable' => true,        'show_ui' => true,         'show_in_menu' => true,         'query_var' => true,        'rewrite' => true,        'capability_type' => 'post',        'has_archive' => true,         'hierarchical' => false,        'menu_position' => null,        'supports' => array('title','editor','author','thumbnail','excerpt','comments')      );       register_post_type('book',$args);    }    ?>  

对于args数组,比较重要,参数比较多,详细说明如下:

label - (字符串,可选,不重要)默认和$post_type一样
labels - (数组,可选) 用来配置文章类型显示在后台的一些描述性文字。默认为空。

  • 'name' - 文章类型的名称,这个可以用中文(一般为复数,对于中文而言就无复数之说了)。
  • 'singular_name'-单篇文章对象的名称,(对于英文而言就是name的单数),默认为name的值
  • 'add_new'-对应于默认文章类型中的“写文章”
  • 'add_new_item'-
  • 'edit_item'-编辑
  • 'new_item'
  • 'view_item'
  • 'search_items'
  • 'not_found'
  • 'not_found_in_trash'
  • 'parent_item_colon'
  • 'menu_name'

description-一些简短的介绍文字

public-(布尔值),用于定义publicly_queriable, show_ui, show_in_nav_menus and exclude_from_search的值

publicly_queryable- (布尔值)可以从前台获取的变量(从url中,比如url重写)

exclude_from_search - (布尔值),是否能够被搜索到

show_ui -  (布尔值)是否生成一个默认的管理页面,也就是是否在后台有管理页面。默认跟public的是一样

show_in_menu -  是否在后台菜单项中显示,如果为ture,那么show_ui的值也必须设置为true,将会有一个顶级菜单项。还可以为一个字符串,类似'tools.php' 或者'edit.php?post_type=page'

menu_position - 在后台菜单中的位置。

menu_icon - 菜单的icon图标(一个url)。

capability_type - 查看、编辑、删除的能力类型(capability),默认为post

capabilities - (数组,一般人用不到)

map_meta_cap - (布尔值),只有设置了capabilities才用的上

hierarchical - (布尔值),文章是否有层级关系,也就是是否允许有父级文章。

supports - (数组),对文章类型的一些功能支持

  • 'title'-标题
  • 'editor' (content) - 内容编辑器
  • 'author' - 作者
  • 'thumbnail' - 特色图像,主题还得支持特色图像才行
  • 'excerpt' - 摘要
  • 'trackbacks'
  • 'custom-fields'-自定义字段
  • 'comments' - 评论
  • 'revisions' - 修订版
  • 'page-attributes' - 页面属性,类似page,选择页面模板的那个

register_meta_box_cb - 当执行remove_meta_box() 和add_meta_box()时调用的函数

taxonomies - 添加已经注册了的分类法(比如默认的分类、标签)

permalink_epmask

has_archive - 文章是否有归档,就是一个所有文章归档页面。

rewrite - (布尔值或者数组),是否有url重写,设置为false的话将会防止url重写,关于重写以后教程详细讲解。

query_var - url重写会用到

can_export - 是否输出

show_in_nav_menus - 是否出现在设置菜单页面的选项中

_builtin - wordpress开发人员建议你不要使用这个参数哦。

_edit_link -  wordpress开发人员建议你不要使用这个参数哦

说实话,上面参数这样列出来,我自己看了都不能很清楚的说明每个参数具体效果是什么,那么下面通过一个简单的实例来使用说明一下上面部分参数的实际效果,我们还是使用wordpress的默认主题,twentyten来测试,在twentyten主题文件夹下新建一个post-type.PHP文件,然后在functions.php文件的最后面添加代码:

<?php    register_post_type( $post_type, $args );    //$post_type-必需,文章类型的名称    //args-可选,一个配置数组    ?>   include_once('post-type.php');   <?php    add_action('init', 'my_custom_init');    function my_custom_init()     {      $labels = array(        'name' => '书本name',        'singular_name' => '书本singularname',        'add_new' => 'Add_new',        'add_new_item' => 'add_new_item',        'edit_item' => 'edit_item',        'new_item' => 'new_item',        'view_item' => 'view_item',        'search_items' => 'search_items',        'not_found' =>  'not_found',        'not_found_in_trash' => 'not_found_in_trash',         'parent_item_colon' => '',        'menu_name' => 'menu_name'         );      $args = array(        'labels' => $labels,        'public' => true,        'publicly_queryable' => true,        'show_ui' => true,         'show_in_menu' => true,         'query_var' => true,        'rewrite' => true,        'capability_type' => 'post',        'has_archive' => true,         'hierarchical' => false,        'menu_position' => null,        'supports' => array('title','editor','author','thumbnail','excerpt','comments')      );       register_post_type('book',$args);    }    ?>  

在post-type.php中我们就可以添加注册文章类型的代码了,post-type.php中添加如下代码:

<?php    register_post_type( $post_type, $args );    //$post_type-必需,文章类型的名称    //args-可选,一个配置数组    ?>   include_once('post-type.php');   <?php    add_action('init', 'my_custom_init');    function my_custom_init()     {      $labels = array(        'name' => '书本name',        'singular_name' => '书本singularname',        'add_new' => 'Add_new',        'add_new_item' => 'add_new_item',        'edit_item' => 'edit_item',        'new_item' => 'new_item',        'view_item' => 'view_item',        'search_items' => 'search_items',        'not_found' =>  'not_found',        'not_found_in_trash' => 'not_found_in_trash',         'parent_item_colon' => '',        'menu_name' => 'menu_name'         );      $args = array(        'labels' => $labels,        'public' => true,        'publicly_queryable' => true,        'show_ui' => true,         'show_in_menu' => true,         'query_var' => true,        'rewrite' => true,        'capability_type' => 'post',        'has_archive' => true,         'hierarchical' => false,        'menu_position' => null,        'supports' => array('title','editor','author','thumbnail','excerpt','comments')      );       register_post_type('book',$args);    }    ?>  

添加上面的代码后,进入后台,部分效果如图:

wordpress进阶教程(二):注册一个自定义的文章类型

 限于篇幅,这篇文章就到这里了。。敬请关注接下来的教程。。