什么是 Post Type ?
WordPress 里面内置了两种常用的 Post Type :Post(文章)、Page(页面),分别用来展示两种类型的内容,一种是相对动态的文章,另一种是相对固定的页面。
创建一个新的 Post Type 需要使用 register_post_type 函数来注册一下。需要在你主题的 functions.php
文件下调用该函数:
register_post_type( $post_type, $args );
$post_type 参数就是你自定义 Post Type 的名称,Post Type 可以自定义的功能非常多,所以这个函数里面的 $args 参数会很多。所以通常会用下面这种格式来注册:
function my_custom_post_product() {
$args = array();
register_post_type( 'product', $args );
}
add_action( 'init', 'my_custom_post_product' );
自定义文章类型代码示例
这是我的博客新增了一个说说的自定义文章类型,是使用Custom Post Type UI插件生成自定义文章类型,代码如下:
function my_shuoshuo()
{
/**
* Post Type: 说说.
*/
$labels = [
"name" => __( "说说", "custom-post-type-ui" ),
"singular_name" => __( "说说", "custom-post-type-ui" ),
"menu_name" => __( "说说", "custom-post-type-ui" ),
"all_items" => __( "所有说说", "custom-post-type-ui" ),
"add_new" => __( "发表说说", "custom-post-type-ui" ),
"add_new_item" => __( "发表说说", "custom-post-type-ui" ),
"edit_item" => __( "编辑说说", "custom-post-type-ui" ),
"new_item" => __( "新说说", "custom-post-type-ui" ),
"view_item" => __( "查看说说", "custom-post-type-ui" ),
"view_items" => __( "查看说说", "custom-post-type-ui" ),
"search_items" => __( "搜索说说", "custom-post-type-ui" ),
"not_found" => __( "暂无说说", "custom-post-type-ui" ),
"not_found_in_trash" => __( "没有已遗弃的说说", "custom-post-type-ui" ),
"parent" => __( "说说:", "custom-post-type-ui" ),
];
$args = [
"label" => __( "说说", "custom-post-type-ui" ),
"labels" => $labels,
"description" => "",
"public" => true,
"publicly_queryable" => true,
"show_ui" => true,
"show_in_rest" => true,
"has_archive" => true,
"show_in_menu" => true,
"show_in_nav_menus" => true,
"delete_with_user" => false,
"exclude_from_search" => true,
"capability_type" => "post",
"map_meta_cap" => true,
"hierarchical" => false,
"rewrite" => false,
"query_var" => true,
"supports" => [ "title", "editor" ],
];
register_post_type( "shuoshuo", $args );
}
add_action('init', 'my_shuoshuo');
$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的详细参数
- labels – (数组,可选) 用来配置文章类型显示在后台的一些描述性文字,默认为空。(上面代码中,为了清晰所以单独拿出来创建了一个数组 $labels)
- 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‘
- show_in_rest – 是否在REST API中添加post type类型。将其设置为true可使帖子类型在块编辑器中可用。
- 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重写;设置数组时参数:slug:默认使用此类型的name,自定义使用array(‘slug’=>$slug);with_front:true或false,允许在连接上添加前缀,默认为true;feeds:默认是has_archive的值;pages:默认值true。关于重写以后教程详细讲解。
- query_var – url重写会用到
- can_export – 是否输出
- show_in_nav_menus – 是否出现在设置菜单页面的选项中
- _builtin – wordpress开发人员建议你不要使用这个参数哦。
- _edit_link – wordpress开发人员建议你不要使用这个参数哦
post type自定义文章类型使用的编辑器
wordpress5.0升级以后,添加了块编辑器(Gutenberg古腾堡编辑器),show_in_rest参数如果设置为false,那么将使用经典编辑器,如果设置为true,那么会使用块编辑器(Gutenberg古腾堡编辑器)。这是因为块编辑器(Gutenberg)完全由REST API提供支持。如果无法通过REST访问帖子类型,则块编辑器无法加载或保存帖子。
自定义 Post Type 的固定连接
Post Type 的参数数组里面的 rewrite
参数可以设置它的固定连接,常用以下几两项:
slug
=>自定义固定连接结构别名,默认是使用 Post Type 名(例如本例的 shuoshuo)。with_front
=> 固定连接是否以根目录为基础路径。如果你在固定连接设置页面设置你的结构为/archives/
,那么你的 Post Type 生成的连接默认为/archives/shuoshuo
如果设置该项为false
即为/shuoshuo
直接基于根路径生成固定连接。
编辑器、固定连接别名问题
我在使用过程中遇到个问题,就是当show_in_rest为false使用经典编辑器时,发表文章,Wenprise Pinyin Slug插件可以对固定连接转换成拼音别名。当show_in_rest为true使用块编辑器时,Wenprise Pinyin Slug插件不起作用。但是默认的标准文章使用块编辑器,插件可以正常生成拼音别名固定连接。
此问题留待大神解决。