WordPress 自定义文章类型是什么?这有终极教程

2018-12-2209:42:08网站建设与开发Comments2,352 views字数 7272阅读模式

是否了解文章类型、自定义分类方法是判断一个开发者是否已经入门 WordPress 开发的一个简单方法,会添加自定义文章类型和分类方法,并在此基础上进行主题或插件开发则是进阶为高级 WordPress 开发者的必经之路。这个说明是使用 register_post_type() 函数注册 WordPress 自定义文章类型的终极教程,看完这个教程,其他类似的都不用看了。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/cms/8978.html

下面是我们在一个WordPress CRM 系统中注册的文章类型、大家随意感受一下。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/cms/8978.html

WordPress 自定义文章类型是什么?这有终极教程文章源自菜鸟学院-https://www.cainiaoxueyuan.com/cms/8978.html

自定义文章类型到底是个什么东西

对,这个问题很重要,了解了自定义文章类型是个什么东西,我们就看清了自定义文章类型,接下来要不要继续交往下去,就看你的口味了。事实上,自定义文章类型不是个东西,他是个类,名字叫 “WP_Post_Type” (真是直白),我们使用自定义文章类型的时候,要 new 出一个这个类的对象来(要是女朋友也能这样来,啧啧…),有了对象,接来了的操作就是围绕着这个对象进行的了,一般我们不直接使用 new WP_Post_Type 方法来创建对象,而是使用 register_post_type() 函数。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/cms/8978.html

首先,我们来看一下 register_post_type() 函数支持的参数

下面是 register_post_type() 函数的参数,根据需要调整这些参数,我们就可以根据我们的需要,注册一个恰到好处的文章类型。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/cms/8978.html

<?php

# 在 'init' 钩子上注册自定义文章类型.
add_action('init', 'my_register_post_types');

/**
 * 注册插件需要的文章类型
 *
 * @since  1.0.0
 * @access public
 * @return void
 */
function my_register_post_types()
{

    // 设置文章类型参数
    $args = [

        // 文章类型的简介,貌似没有在 WordPress 内核中使用,不过我们可以在主题或插件中使用
        'description'         => __('This is a description for my post type.', 'wprs'),
        // 字符串

        // 文章类型是否公开给管理员或者前端用户使用,这个参数的值是后面很多参数的默认值
        'public'              => true,
        // bool (default is FALSE)

        // 是否可以在前端作为 parse_request() 的一部分查询该文章类型
        'publicly_queryable'  => true,
        // bool (默认为 'public' 参数的值).

        // 是否在前端搜索中隐藏该文章类型
        'exclude_from_search' => false,
        // bool (默认为 'public' 反值)

        // 是否可以在导航菜单中选择
        'show_in_nav_menus'   => false,
        // bool (默认为 'public' 参数的值)

        // 是否在管理界面生成默认的管理界面,使用后面的参数,可以控制生成的 UI 组件,如果我们要构建自己的管理界面,
        //设置该参数为 False
        'show_ui'             => true,
        // bool (默认为 'public' 的值)

        // 是否在管理菜单中显示,'show_ui' 参数必须设置为 True,这个参数才有效,我们页可以设置该参数为一个顶级菜单
        //(如:'tools.php'),这种情况下,该文章类型的管理菜单出现在 Tools 菜单下面
        'show_in_menu'        => true,
        // bool (默认为 'show_ui' 的值)

        // 是否在管理工具条中显示该文章类型,如果设置为 true,WordPress 会在管理工具条中添加一个新建该文章类型文章的链接
        'show_in_admin_bar'   => true,
        // bool (默认为 'show_in_menu' 的值)

        // 该文章类型在管理菜单中出现的位置,'show_in_menu' 必须设置为 true,该参数才有用
        'menu_position'       => null,
        // int (默认为 25 - 出现在「评论」菜单后面)

        // 管理菜单的图标 URI,或者 Dashicon 的类名称. 参见: https://developer.wordpress.org/resource/dashicons/
        'menu_icon'           => null,
        // 字符串 (默认使用文章图标)

        // 属于该文章类型的文章是否可以通过 WordPress 导入/导出插件或者类型的插件导出
        'can_export'          => true,
        // bool (默认为 TRUE)

        // 是否暴露在 Rest API 中
        'show_in_rest',
        // 布尔值,默认为 false

        // 使用 Rest API 访问的基础 URI 别名
        'rest_base',
        // 字符串,默认为文章类型别名

        // 使用自定义 Rest API 控制器而不是默认的 WP_REST_Posts_Controller,自定义控制器必须继承 WP_REST_Controller
        'rest_controller_class',
        // 字符串,默认为 WP_REST_Posts_Controller

        // 是否在删除用户时,删除他们撰写的文章
        'delete_with_user'    => false,
        // bool (如果文章类型支持 ‘author’ 功能,该参数默认为 TRUE)

        // 该文章类型是否支持多级文章(父级文章/子文章/等等.) 
        'hierarchical'        => false,
        // bool (默认为 FALSE)

        // 是否为该文章类型开启存档页面 index/archive/root 页面,如果设置为 TRUE, 该文章类型名称将作为存档页面别名使用,
        //当然,我们页可以设置自定义存档别名
        'has_archive'         => 'example',
        // bool|string (默认为 FALSE)

        // 为该文章类型设置 query_var 键,如果设置为 TRUE, 将使用文章类型名称,如果需要,也可以设置自定义字符串
        'query_var'           => 'example',
        // bool|string (默认为 TRUE - 文章类型名称)

        // 用于构建该文章类型的编辑、删除、阅读权限的字符串,可以设置字符串或者数组,如果单词的负数不是加“s”的形式,我们需要
        //设置一个数组,array( 'box', 'boxes' )
        'capability_type'     => 'example',
        // string|array (默认为 'post')

        // 是否让 WordPress 映射权限元数据 (edit_post, read_post, delete_post),如果设置为 FALSE, 我们需要自己通过
        //过滤 “map_meta_cap” 钩子来设置文章类型权限
        'map_meta_cap'        => true,
        // bool (默认为 FALSE)

        // 设置更精确的文章类型权限,WordPress 默认使用 'capability_type' 参数来构建权限,多数情况下,我们不需要像文章
        //或页面这么完整的权限,下面是我经常使用的几个权限: 'manage_examples', 'edit_examples', 'create_examples'.
        // 每个文章类型都是独特的,我们可以根据需要调整这些权限
        'capabilities'        => [

            // meta caps (don't assign these to roles)
            'edit_post'              => 'edit_example',
            'read_post'              => 'read_example',
            'delete_post'            => 'delete_example',

            // primitive/meta caps
            'create_posts'           => 'create_examples',

            // primitive caps used outside of map_meta_cap()
            'edit_posts'             => 'edit_examples',
            'edit_others_posts'      => 'manage_examples',
            'publish_posts'          => 'manage_examples',
            'read_private_posts'     => 'read',

            // primitive caps used inside of map_meta_cap()
            'read'                   => 'read',
            'delete_posts'           => 'manage_examples',
            'delete_private_posts'   => 'manage_examples',
            'delete_published_posts' => 'manage_examples',
            'delete_others_posts'    => 'manage_examples',
            'edit_private_posts'     => 'edit_examples',
            'edit_published_posts'   => 'edit_examples',
        ],

        // 定义该文章类型的 URL 结构,我们可以设置一个具体的参数或一个布尔值,如果设置为 false,该文章类型将不支持
        // URL Rewrite 功能
        'rewrite'             => [

            // 文章类型的别名
            'slug'       => 'example', // string (默认为文章类型名称)

            // 是否在固定链接中显示 $wp_rewrite->front 文章类型别名
            'with_front' => false, // bool (默认为 TRUE)

            // 是否允许文章类型中的文章通过 <!--nextpage--> 快捷标签实现分页
            'pages'      => true, // bool (默认为 TRUE)

            // 是否为订阅源创建漂亮的固定链接feeds.
            'feeds'      => true, // bool (默认为 'has_archive' 的值)

            // 为固定链接设置设置 endpoint 遮罩
            'ep_mask'    => EP_PERMALINK, // const (默认为 EP_PERMALINK)
        ],

        // 文章类型支持的 WordPress 功能,许多参数在文章编辑界面非常有用。这有助于其他主题和插件决定让用户使用什么功能
        //或者提供什么数据,我们可以为该参数设置一个数组,也可以设置为 false,以防止添加任何功能,文章类型创建后,我们
        //可以使用 add_post_type_support() 添加功能,或使用 remove_post_type_support() 删除功能。默认功能是“标题
        //”和“编辑器”。
        'supports'            => [
            'title',// 文章标题 ($post->post_title).
            'editor', // 文章内容 ($post->post_content).
            'excerpt', // 文章摘要 ($post->post_excerpt).
            'author',  // 文章作者 ($post->post_author).
            'thumbnail',// 特色图像 (当前站点使用的主题必须支持 'post-thumbnails').
            'comments', // 显示评论元数据盒子,如果设置了该值, 这个文章类型将支持评论
            'trackbacks', // 在编辑界面显示允许发送链接通知的元数据盒子
            'custom-fields', // 显示自定义字段元数据盒子
            'revisions', // 显示版本元数据盒子,如果设置了该参数,WordPress 将在数据库中保存文章版本
            'page-attributes',  // 显示“页面属性”元数据盒子,包含父级页面或页面排序字段。
            'post-formats',// 显示文章格式元数据盒子,并允许该文章类型使用文章格式
        ],
        // 标签用来在管理界面或前端显示该文章类型的名称,标签参数不会自动改写文章更新、错误等信息中的字段,我们需要过滤
        // 'post_updated_messages' 钩子来自定义这些消息。
        'labels'              => [
            'name'                  => __('Posts', 'wprs'),
            'singular_name'         => __('Post', 'wprs'),
            'menu_name'             => __('Posts', 'wprs'),
            'name_admin_bar'        => __('Posts', 'wprs'),
            'add_new'               => __('Add New', 'wprs'),
            'add_new_item'          => __('Add New Post', 'wprs'),
            'edit_item'             => __('Edit Post', 'wprs'),
            'new_item'              => __('New Post', 'wprs'),
            'view_item'             => __('View Post', 'wprs'),
            'search_items'          => __('Search Posts', 'wprs'),
            'not_found'             => __('No posts found', 'wprs'),
            'not_found_in_trash'    => __('No posts found in trash', 'wprs'),
            'all_items'             => __('All Posts', 'wprs'),
            'featured_image'        => __('Featured Image', 'wprs'),
            'set_featured_image'    => __('Set featured image', 'wprs'),
            'remove_featured_image' => __('Remove featured image', 'wprs'),
            'use_featured_image'    => __('Use as featred image', 'wprs'),
            'insert_into_item'      => __('Insert into post', 'wprs'),
            'uploaded_to_this_item' => __('Uploaded to this post', 'wprs'),
            'views'                 => __('Filter posts list', 'wprs'),
            'pagination'            => __('Posts list navigation', 'wprs'),
            'list'                  => __('Posts list', 'wprs'),

            // 只在分级文章类型中使用的标签
            'parent_item'           => __('Parent Post', 'wprs'),
            'parent_item_colon'     => __('Parent Post:', 'wprs'),
        ],
    ];

    // 注册文章类型
    register_post_type(
        'example', // 文章类型名称,最多 20 个字符,不支持大写或空格
        $args // 文章类型的参数
    );

}

通过 register_post_type_args 过滤器修改已注册的文章类型

上文是使用 register_post_type() 函数注册一个全新的文章类型,如果我们需要一个已经注册过的文章类型呢?WordPress 为我们提供了一个过滤器“register_post_type_args”,使用这个过滤器,我们可以修改现有文章类型的参数,从而达到修改已注册的文章类型的目的。比如下面的示例中,我们修改了 “order” 文章类型的参数为 false,来实现在界面中隐藏该文章类型的目的。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/cms/8978.html

add_filter('register_post_type_args', function ($args, $post_type)
{
    if ($post_type == 'order') {
        $args[ 'public' ] = false;
    }

    return $args;
});

使用辅助函数修改已注册的文章类型

除了使用上面的过滤器方法修改现有的文章类型,WordPress 也为我们提供了几个函数来实现常用的文章类型修改需求。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/cms/8978.html

add_post_type_support( 'post_type_name', array( 'title', 'editor' ) ); // 添加文章类型支持
register_taxonomy_for_object_type( 'taxonomy_name', 'post_type_name' ); //注册自定义分类法
unregister_taxonomy_for_object_type( 'taxonomy_name', 'post_type_name' ); //移除自定义分类法

反注册/删除已注册的文章类型

有正就又反,有注册就有删除,我们可以使用下面的函数,移除现有的文章类型文章源自菜鸟学院-https://www.cainiaoxueyuan.com/cms/8978.html

unregister_post_type('post_type_name')

使用自定义文章类型搞一些事情

有了自定义文章类型,放着不用也是浪费,我们还可以借助下面的函数使用自定义文章类型搞一些事情。篇幅所限,这些函数就不提供参数解读了,如有需要,请搜索查看文档,如果找不到文档,翻一下源码也是可以的。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/cms/8978.html

判断文章类型的性质

is_post_type_hierarchical( $post_type ) // 判断是否为分级文章类型
post_type_exists( $post_type ) // 判断文章类型是否存在
post_type_supports( $post_type, $feature ) // 判断文章类型是否支持某功能
is_post_type_viewable( $post_type ) // 判断文章类型是否可以在前端查看

获取文章类型对象及其属性

get_post_type( $post = null ) // 判断指定文章的文章类型
get_post_type_object( $post_type ) // 获取文章类型对象
get_post_types( $args = array(), $output = 'names', $operator = 'and' ) // 获取所有文章类型
get_post_type_labels( $post_type_object ) // 获取所有文章类型标签
get_all_post_type_supports( $post_type ) //获取文章类型支持的所有功能
get_post_types_by_support( $feature, $operator = 'and' ) // 获取支持某功能的所有文章类型
set_post_type( $post_id = 0, $post_type = 'post' ) // 设置文章的文章类型
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/cms/8978.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/cms/8978.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定