首页 WordPress函数大全 register_post_type()

register_post_type()

2020-05-02 / 4686阅 / 悠然

如何你看完本文还不知道如何使用该函数,可以联系我定制视频教程,50元/个函数,学不会全额退款

register_post_type( string $post_type, array|string $args = array() )

注册帖子类型。

描述

注意:帖子类型注册不应在执行“ init”操作之前挂接。同样,任何分类法连接都应通过$ taxonomies参数进行注册,以确保使用钩子(例如“ parse_query”或“ pre_get_posts”)时的一致性。

帖子类型可以支持任意数量的内置核心功能,例如元框,自定义字段,帖子缩略图,帖子状态,评论等。有关支持的功能的完整列表,请参见$ supports参数。

参数

$post_type

(string)
(Required)
Post type key. Must not exceed 20 characters and may >sanitize_key().

$args

(array|string)
(Optional)
Array or string of arguments for registering a post type.

  • 'label'
    (string) Name of the post type shown in the menu. Usually plural. Default is value of $labels['name'].
  • 'labels'
    (array) An array of labels for this post type. If not set, post labels are inherited for non-hierarchical types and page labels for hierarchical>get_post_type_labels() for a full list of supported labels.
  • 'description'
    (string) A short descriptive summary of what the post type is.
  • 'public'
    (bool) Whether a post type is intended for use publicly either via the admin interface or by front-end users. While the default settings of $exclude_from_search, $publicly_queryable, $show_ui, and $show_in_nav_menus are inherited from public, each does not rely>WP_REST_Posts_Controller'.
  • 'menu_position'
    (int) The position in the menu order the post type should appear. To work, $show_in_menu must be true. Default null (at the bottom).
  • 'menu_icon'
    (string) The url to the icon to be used for this menu. Pass a base64-encoded SVG using a data URI, which will be colored to match the color scheme -- this should begin with 'data:image/svg+xml;base64,'. Pass the name of a Dashicons helper class to use a font icon, e.g. 'dashicons-chart-pie'. Pass 'none' to leave div.wp-menu-image empty so an icon can be added via CSS. Defaults to use the posts icon.
  • 'capability_type'
    (string) The string to use to build the read, edit, and delete capabilities. May be passed as an array to allow for alternative plurals when using this argument as a base to construct the capabilities, e.g. array('story', 'stories'). Default 'post'.
  • 'capabilities'
    (array) Array of capabilities for this post type. $capability_type is used as a base to construct capabilities by default. See get_post_type_capabilities().
  • 'map_meta_cap'
    (bool) Whether to use the internal default meta capability handling. Default false.
  • 'supports'
    (array) Core feature(s) the post type supports. Serves as an alias for calling add_post_type_support() directly. Core features include 'title', 'editor', 'comments', 'revisions', 'trackbacks', 'author', 'excerpt', 'page-attributes', 'thumbnail', 'custom-fields', and 'post-formats'. Additionally, the 'revisions' feature dictates whether the post type will store revisions, and the 'comments' feature dictates whether the comments count will show>remove_meta_box() and add_meta_box() calls in the callback. Default null.
  • 'taxonomies'
    (array) An array of taxonomy identifiers that will be registered for the post type. Taxonomies can be registered later with register_taxonomy() or register_taxonomy_for_object_type().
  • 'has_archive'
    (bool|string) Whether there should be post type archives, or if a string, the archive slug to use. Will generate the proper rewrite rules if $rewrite is enabled. Default false.
  • 'rewrite'
    (bool|array) Triggers the handling of rewrites for this post type. To prevent rewrite, set to false. Defaults to true, using $post_type as slug. To specify rewrite rules, an array can be passed with any of these keys:

    • 'slug'
      (string) Customize the permastruct slug. Defaults to $post_type key.
    • 'with_front'
      (bool) Whether the permastruct should be prepended with WP_Rewrite::$front. Default true.
    • 'feeds'
      (bool) Whether the feed permastruct should be built for this post type. Default is value of $has_archive.
    • 'pages'
      (bool) Whether the permastruct should provide for pagination. Default true.
    • 'ep_mask'
      (const) Endpoint mask to assign. If not specified and permalink_epmask is set, inherits from $permalink_epmask. If not specified and permalink_epmask is not set, defaults to EP_PERMALINK.
  • 'query_var'
    (string|bool) Sets the query_var key for this post type. Defaults to $post_type key. If false, a post type cannot be loaded at ?{query_var}={post_slug}. If specified as a string, the query ?{query_var_string}={post_slug} will be valid.
  • 'can_export'
    (bool) Whether to allow this post type to be exported. Default true.
  • 'delete_with_user'
    (bool) Whether to delete posts of this type when deleting a user. If true, posts of this type belonging to the user will be moved to Trash when then user is deleted. If false, posts of this type belonging to the user will *not* be trashed or deleted. If not set (the default), posts are trashed if post_type_supports('author'). Otherwise posts are not trashed or deleted. Default null.
  • '_builtin'
    (bool) FOR INTERNAL USE>Default value: array()

返回

(WP_Post_Type|WP_Error) The registered post type object>WP_Error object on failure.

大家谈论
    我的见解