详细的WordPress 自定义文章类型终极教程——注册到修改到删除

自定义文章类型可以帮助我们快速的用WordPress实现很多项目开发,比如项目管理、CRM系统等,多个自定义文章类型相互关联使用非常好用,但是里面的参数太多,这里详细说一下具体每一个参数的用法

  1. <?php
  2.  
  3. # 在 'init' 钩子上注册自定义文章类型.
  4. add_action('init', 'my_register_post_types');
  5.  
  6. /**
  7.  * 注册插件需要的文章类型
  8.  *
  9.  * @since  1.0.0
  10.  * @access public
  11.  * @return void
  12.  */
  13. function my_register_post_types()
  14. {
  15.  
  16.     // 设置文章类型参数
  17.     $args = [
  18.  
  19.         // 文章类型的简介,貌似没有在 WordPress 内核中使用,不过我们可以在主题或插件中使用
  20.         'description'         => __('This is a description for my post type.', 'wprs'),
  21.         // 字符串
  22.  
  23.         // 文章类型是否公开给管理员或者前端用户使用,这个参数的值是后面很多参数的默认值
  24.         'public'              => true,
  25.         // bool (default is FALSE)
  26.  
  27.         // 是否可以在前端作为 parse_request() 的一部分查询该文章类型
  28.         'publicly_queryable'  => true,
  29.         // bool (默认为 'public' 参数的值).
  30.  
  31.         // 是否在前端搜索中隐藏该文章类型
  32.         'exclude_from_search' => false,
  33.         // bool (默认为 'public' 反值)
  34.  
  35.         // 是否可以在导航菜单中选择
  36.         'show_in_nav_menus'   => false,
  37.         // bool (默认为 'public' 参数的值)
  38.  
  39.         // 是否在管理界面生成默认的管理界面,使用后面的参数,可以控制生成的 UI 组件,如果我们要构建自己的管理界面,
  40.         //设置该参数为 False
  41.         'show_ui'             => true,
  42.         // bool (默认为 'public' 的值)
  43.  
  44.         // 是否在管理菜单中显示,'show_ui' 参数必须设置为 True,这个参数才有效,我们页可以设置该参数为一个顶级菜单
  45.         //(如:'tools.php'),这种情况下,该文章类型的管理菜单出现在 Tools 菜单下面
  46.         'show_in_menu'        => true,
  47.         // bool (默认为 'show_ui' 的值)
  48.  
  49.         // 是否在管理工具条中显示该文章类型,如果设置为 true,WordPress 会在管理工具条中添加一个新建该文章类型文章的链接
  50.         'show_in_admin_bar'   => true,
  51.         // bool (默认为 'show_in_menu' 的值)
  52.  
  53.         // 该文章类型在管理菜单中出现的位置,'show_in_menu' 必须设置为 true,该参数才有用
  54.         'menu_position'       => null,
  55.         // int (默认为 25 - 出现在「评论」菜单后面)
  56.  
  57.         // 管理菜单的图标 URI,或者 Dashicon 的类名称. 参见: https://developer.wordpress.org/resource/dashicons/
  58.         'menu_icon'           => null,
  59.         // 字符串 (默认使用文章图标)
  60.  
  61.         // 属于该文章类型的文章是否可以通过 WordPress 导入/导出插件或者类型的插件导出
  62.         'can_export'          => true,
  63.         // bool (默认为 TRUE)
  64.  
  65.         // 是否暴露在 Rest API 中
  66.         'show_in_rest',
  67.         // 布尔值,默认为 false
  68.  
  69.         // 使用 Rest API 访问的基础 URI 别名
  70.         'rest_base',
  71.         // 字符串,默认为文章类型别名
  72.  
  73.         // 使用自定义 Rest API 控制器而不是默认的 WP_REST_Posts_Controller,自定义控制器必须继承 WP_REST_Controller
  74.         'rest_controller_class',
  75.         // 字符串,默认为 WP_REST_Posts_Controller
  76.  
  77.         // 是否在删除用户时,删除他们撰写的文章
  78.         'delete_with_user'    => false,
  79.         // bool (如果文章类型支持 ‘author’ 功能,该参数默认为 TRUE)
  80.  
  81.         // 该文章类型是否支持多级文章(父级文章/子文章/等等.) 
  82.         'hierarchical'        => false,
  83.         // bool (默认为 FALSE)
  84.  
  85.         // 是否为该文章类型开启存档页面 index/archive/root 页面,如果设置为 TRUE, 该文章类型名称将作为存档页面别名使用,
  86.         //当然,我们页可以设置自定义存档别名
  87.         'has_archive'         => 'example',
  88.         // bool|string (默认为 FALSE)
  89.  
  90.         // 为该文章类型设置 query_var 键,如果设置为 TRUE, 将使用文章类型名称,如果需要,也可以设置自定义字符串
  91.         'query_var'           => 'example',
  92.         // bool|string (默认为 TRUE - 文章类型名称)
  93.  
  94.         // 用于构建该文章类型的编辑、删除、阅读权限的字符串,可以设置字符串或者数组,如果单词的负数不是加“s”的形式,我们需要
  95.         //设置一个数组,array( 'box', 'boxes' )
  96.         'capability_type'     => 'example',
  97.         // string|array (默认为 'post')
  98.  
  99.         // 是否让 WordPress 映射权限元数据 (edit_post, read_post, delete_post),如果设置为 FALSE, 我们需要自己通过
  100.         //过滤 “map_meta_cap” 钩子来设置文章类型权限
  101.         'map_meta_cap'        => true,
  102.         // bool (默认为 FALSE)
  103.  
  104.         // 设置更精确的文章类型权限,WordPress 默认使用 'capability_type' 参数来构建权限,多数情况下,我们不需要像文章
  105.         //或页面这么完整的权限,下面是我经常使用的几个权限: 'manage_examples', 'edit_examples', 'create_examples'.
  106.         // 每个文章类型都是独特的,我们可以根据需要调整这些权限
  107.         'capabilities'        => [
  108.  
  109.             // meta caps (don't assign these to roles)
  110.             'edit_post'              => 'edit_example',
  111.             'read_post'              => 'read_example',
  112.             'delete_post'            => 'delete_example',
  113.  
  114.             // primitive/meta caps
  115.             'create_posts'           => 'create_examples',
  116.  
  117.             // primitive caps used outside of map_meta_cap()
  118.             'edit_posts'             => 'edit_examples',
  119.             'edit_others_posts'      => 'manage_examples',
  120.             'publish_posts'          => 'manage_examples',
  121.             'read_private_posts'     => 'read',
  122.  
  123.             // primitive caps used inside of map_meta_cap()
  124.             'read'                   => 'read',
  125.             'delete_posts'           => 'manage_examples',
  126.             'delete_private_posts'   => 'manage_examples',
  127.             'delete_published_posts' => 'manage_examples',
  128.             'delete_others_posts'    => 'manage_examples',
  129.             'edit_private_posts'     => 'edit_examples',
  130.             'edit_published_posts'   => 'edit_examples',
  131.         ],
  132.  
  133.         // 定义该文章类型的 URL 结构,我们可以设置一个具体的参数或一个布尔值,如果设置为 false,该文章类型将不支持
  134.         // URL Rewrite 功能
  135.         'rewrite'             => [
  136.  
  137.             // 文章类型的别名
  138.             'slug'       => 'example', // string (默认为文章类型名称)
  139.  
  140.             // 是否在固定链接中显示 $wp_rewrite->front 文章类型别名
  141.             'with_front' => false, // bool (默认为 TRUE)
  142.  
  143.             // 是否允许文章类型中的文章通过 <!--nextpage--> 快捷标签实现分页
  144.             'pages'      => true, // bool (默认为 TRUE)
  145.  
  146.             // 是否为订阅源创建漂亮的固定链接feeds.
  147.             'feeds'      => true, // bool (默认为 'has_archive' 的值)
  148.  
  149.             // 为固定链接设置设置 endpoint 遮罩
  150.             'ep_mask'    => EP_PERMALINK, // const (默认为 EP_PERMALINK)
  151.         ],
  152.  
  153.         // 文章类型支持的 WordPress 功能,许多参数在文章编辑界面非常有用。这有助于其他主题和插件决定让用户使用什么功能
  154.         //或者提供什么数据,我们可以为该参数设置一个数组,也可以设置为 false,以防止添加任何功能,文章类型创建后,我们
  155.         //可以使用 add_post_type_support() 添加功能,或使用 remove_post_type_support() 删除功能。默认功能是“标题
  156.         //”和“编辑器”。
  157.         'supports'            => [
  158.             'title',// 文章标题 ($post->post_title).
  159.             'editor', // 文章内容 ($post->post_content).
  160.             'excerpt', // 文章摘要 ($post->post_excerpt).
  161.             'author',  // 文章作者 ($post->post_author).
  162.             'thumbnail',// 特色图像 (当前站点使用的主题必须支持 'post-thumbnails').
  163.             'comments', // 显示评论元数据盒子,如果设置了该值, 这个文章类型将支持评论
  164.             'trackbacks', // 在编辑界面显示允许发送链接通知的元数据盒子
  165.             'custom-fields', // 显示自定义字段元数据盒子
  166.             'revisions', // 显示版本元数据盒子,如果设置了该参数,WordPress 将在数据库中保存文章版本
  167.             'page-attributes',  // 显示“页面属性”元数据盒子,包含父级页面或页面排序字段。
  168.             'post-formats',// 显示文章格式元数据盒子,并允许该文章类型使用文章格式
  169.         ],
  170.         // 标签用来在管理界面或前端显示该文章类型的名称,标签参数不会自动改写文章更新、错误等信息中的字段,我们需要过滤
  171.         // 'post_updated_messages' 钩子来自定义这些消息。
  172.         'labels'              => [
  173.             'name'                  => __('Posts', 'wprs'),
  174.             'singular_name'         => __('Post', 'wprs'),
  175.             'menu_name'             => __('Posts', 'wprs'),
  176.             'name_admin_bar'        => __('Posts', 'wprs'),
  177.             'add_new'               => __('Add New', 'wprs'),
  178.             'add_new_item'          => __('Add New Post', 'wprs'),
  179.             'edit_item'             => __('Edit Post', 'wprs'),
  180.             'new_item'              => __('New Post', 'wprs'),
  181.             'view_item'             => __('View Post', 'wprs'),
  182.             'search_items'          => __('Search Posts', 'wprs'),
  183.             'not_found'             => __('No posts found', 'wprs'),
  184.             'not_found_in_trash'    => __('No posts found in trash', 'wprs'),
  185.             'all_items'             => __('All Posts', 'wprs'),
  186.             'featured_image'        => __('Featured Image', 'wprs'),
  187.             'set_featured_image'    => __('Set featured image', 'wprs'),
  188.             'remove_featured_image' => __('Remove featured image', 'wprs'),
  189.             'use_featured_image'    => __('Use as featred image', 'wprs'),
  190.             'insert_into_item'      => __('Insert into post', 'wprs'),
  191.             'uploaded_to_this_item' => __('Uploaded to this post', 'wprs'),
  192.             'views'                 => __('Filter posts list', 'wprs'),
  193.             'pagination'            => __('Posts list navigation', 'wprs'),
  194.             'list'                  => __('Posts list', 'wprs'),
  195.  
  196.             // 只在分级文章类型中使用的标签
  197.             'parent_item'           => __('Parent Post', 'wprs'),
  198.             'parent_item_colon'     => __('Parent Post:', 'wprs'),
  199.         ],
  200.     ];
  201.  
  202.     // 注册文章类型
  203.     register_post_type(
  204.         'example', // 文章类型名称,最多 20 个字符,不支持大写或空格
  205.         $args // 文章类型的参数
  206.     );
  207.  
  208. }

相关推荐