wordpress注册新文章类型register_post_type()函数

        这是别人博客上找到的,鲜有这么简单明了的文章来演示wordpress的注册新文章类型,搬运过来,自己开发插件的时候应该会用到的吧。

register_post_type()函数是用于自定义文章类型的函数

语法结构

register_post_type($post_type,$args)

参数

$post_type (string) (必选) 文章类型的名称(最多20个字符) Default: 空
$args (array) (可选) 一个数组参数 Default: 空

Post Type 可以自定义的功能非常多,所以这个函数里面的 $args参数会很多。所以通常会用下面这种格式来注册,也有很多是可选项:

// 注册自定义文章形式function custom_post_type(){
    $labels = array(
        'name' => 'Post Type General Name',
        'singular_name' => 'Post Type Singular Name',
        'menu_name' => 'Post Types',
        'name_admin_bar'  => 'Post Type',
        'archives'  => 'Item Archives',
        'attributes' => 'Item Attributes',
        'parent_item_colon' => 'Parent Item:',
        'all_items' => 'All Items',
        'add_new_item' => 'Add New Item',
        'add_new' => 'Add New',
        'new_item' => 'New Item',
        'edit_item' => 'Edit Item',
        'update_item' => 'Update Item',
        'view_item' => 'View Item',
        'view_items' => 'View Items',
        'search_items' => 'Search Item',
        'not_found' => 'Not found',
        'not_found_in_trash' => 'Not found in Trash',
        'featured_image' => 'Featured Image',
        'set_featured_image' => 'Set featured image',
        'remove_featured_image' => 'Remove featured image',
        'use_featured_image' => 'Use as featured image',
        'insert_into_item' => 'Insert into item',
        'uploaded_to_this_item' => 'Uploaded to this item',
        'items_list' => 'Items list',
        'items_list_navigation' => 'Items list navigation',
        'filter_items_list' => 'Filter items list'
    );
    $args = array(
        'labels' => $labels,
        'description' => 'Post Type Description',
        'supports'  => array('title','editor','author','thumbnail','excerpt','comments'),
        'taxonomies' => array( 'category', 'post_tag' ),
        'hierarchical' => false,
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => 5,
        'menu_icon' => 'dashicons-cart',
        'show_in_admin_bar' => true,
        'show_in_nav_menus' => true,
        'can_export' => true,
        'has_archive' => true,
        'exclude_from_search' => false,
        'publicly_queryable' => true,
        'capability_type' => 'page',
    );
    register_post_type('post_type', $args);}add_action('init', 'custom_post_type');

$labels用来配置文章类型显示在后台的一些描述性文字,默认为空。(上面代码中,为了清晰所以单独拿出来创建了一个数组 $labels)

name – 文章类型的名称(英语写为复数)
singular_name – 单篇文章类型的名称(英语写为复数)
add_new – 对应“添加新的文本”
all_items – 子菜单的字符串。默认是所有帖子/所有页面。
add_new_item – “添加新帖/新页面”
edit_item – “编辑帖子/页面”
new_item – “新贴/新页”
view_item – 用于查看帖子类型归档的标签。默认是’查看帖子’/’查看页面’
search_items – 默认是搜索帖子/搜索页面
not_found – 默认是没有发现帖子/找不到页面。
not_found_in_trash – 默认是在垃圾桶中找不到帖子/在垃圾桶中找不到页面。
parent_item_colon – 此字符串不用于非分层类型。在层次结构中,默认为“父页面:”。
menu_name’ – 菜单名称,默认与`name`相同。

$args的详细参数

description – 一些简短的介绍文字
public- 用于定义publicly_queryable,exclude_from_search,show_ui,show_in_nav_menus可见的方式, 默认是false,’true 的话即为show_ui = true,public_queryable = true,exclude_from_search = false,show_in_nav_menus = true

  1. publicly_queryable – 可以从前台获取的变量(从url中,比如url重写),默认值:public参数的值
  2. exclude_from_search – 是否能够被搜索到。默认值:与public参数相反的值
  3. show_ui – 是否生成一个默认的管理页面,也就是是否在后台有管理页面。默认值:public参数的值
  4. show_in_nav_menus -是否可以在导航菜单中选择post_type。默认值:public参数的值
  5. show_in_menu- 是否在后台菜单项中显示,如果为ture,那么show_ui的值也必须设置为true,将会有一个顶级菜单项。 默认值:null

menu_position – 在后台菜单中的位置

  1. 5 – below Posts
  2. 10 – below Media
  3. 15 – below Links
  4. 20 – below Pages
  5. 25 – below comments
  6. 60 – below first separator
  7. 65 – below Plugins
  8. 70 – below Users
  9. 75 – below Tools
  10. 80 – below Settings
  11. 100 – below second separator

menu_icon-用于此菜单的图标的URL或iconfont中图标的名称  默认值:null – 默认为帖子图标
capability_type – 查看、编辑、删除的能力类型(capability),默认为post
capabilities – 这个帖子类型的功能的数组 (一般人用不到)。默认值:capability_type用于构造
map_meta_cap – 是否使用内部默认元功能处理,只有设置了capabilities才用的上。默认值:false
hierarchical – 文章是否有层级关系,也就是是否允许有父级文章。
supports – 对文章类型的一些功能支持

  1. ‘title’ 标题
  2. ‘editor’ (content) 编辑
  3. ‘author’ 作者
  4. ‘thumbnail’ 特色图
  5. ‘excerpt’ 摘抄
  6. ‘trackbacks’ 引用通过
  7. ‘custom-fields’ 自定义字段
  8. ‘comments’ 评论
  9. ‘revisions’ 修订版
  10. ‘page-attributes’ 页面属性,类似page,选择页面模板的那个

register_meta_box_cb – 提供在设置编辑表单的元框时调用的回调函数。回调函数使用一个参数$ post,其中包含当前编辑的帖子的WP_Post对象。在回调中执行remove_meta_box()和add_meta_box()调用。默认值:无
taxonomies – 添加已经注册了的分类法
has_archive- 文章是否有归档,就
是一个所有文章归档页面
rewrite – 触发此帖子类型的重写操作。为了防止重写,设置为false。默认值:true,并使用$ post_type作为slug
* $ args数组

  1. * ‘slug’=> string自定义永久链接结构块。默认为$ post_type值。应该是可翻译的
  2. * ‘with_front’=> bool应该使用前置基座添加永久链接结构。(例如:如果你的永久链接结构是/ blog /,那么你的链接将是:false – > / news /,true – > / blog / news /)。默认为true
  3. * ‘feed’=> bool应该为此帖子类型构建一个feed permalink结构。默认为has_archive值。
  4. * ‘pages’=> bool应该是永久链接结构提供分页。默认为true

query_var – 设置此帖子类型的query_var键。 默认值:true – 设置为$ post_type false则表示禁用
can_export – 可以导出此post_type。默认值:true

实例

<?phpadd_action('init', 'my_custom_product');function my_custom_product(){
    $labels = array(
        'name' => 'Products Name',
        'singular_name' => 'Product Singular Name',
        'add_new' => '添加产品',
        '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,
        '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' => 5,
        'menu_icon' => 'dashicons-cart',
        'taxonomies'=> array('post_tag'),
        'supports' => array('title','editor','author','thumbnail','excerpt','comments')
    );
    register_post_type('product',$args);
    $labels = array(
        'name' => '产品分类',
        'singular_name' => '产品分类',
        'search_items' =>  '搜索产品' ,
        'all_items' => '所有产品' ,
        'parent_item' => null,
        'parent_item_colon' => null,
        'edit_item' => '编辑产品' ,
        'update_item' => '更新产品' ,
        'add_new_item' => '添加产品' ,
        'new_item_name' => '新产品',
        'separate_items_with_commas' => '按逗号分开' ,
        'add_or_remove_items' => '添加或删除',
        'choose_from_most_used' => '从经常使用的类型中选择',
        'menu_name' => '分类目录',
    );
    register_taxonomy(
        'products',
        array('product'),
        array(
            'hierarchical' => true,
            'labels' => $labels,
            'show_ui' => true,
            'query_var' => true,
        )
    );}?>

这里为了直观方便,我直接使用了中文,更好的应该是使用英文然后通过本地化函数来翻译成中文。通过以上代码我们就可以创建一个名为产品的分类

wordpress注册新文章类型register_post_type()函数  第1张

这种方法前端调用的话只需要创建:archive-product.php 模板即可,这个模板用于分类列表页的模板调用,product是我们的post type的名字。创建 single-product.php 模板即可实现分类的文章的详情页的调用。

‘taxonomies’=> array(‘post_tag’)这段代码意思是给自定义post type添加标签页面,如果需要使用WordPress自带的分类标签方法,可以写为注意,’taxonomies’=> array(‘post_tag’, ‘category’)。

在wordpress中也有一种独立的分类法,与文章类型一样,可以使用register_taxonomy()函数来注册分类方法。

 

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容