WordPress函数:register post type (自定义文章类型)用法和范例

来源: 老季博客
日期: 2018-9-14
作者: 腾讯云/服务器VPS推荐评测/Vultr
阅读数: 49

register post type() 函数支持我们创建一个自定义的文章类型,或者修改已有的文章类型,该函数不能在init动作之前使用。

用法

<?php register_post_type( $post_type, $args ); ?>

参数

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

$args参数说明
label
(string) (可选) 是新类型的别名标记
默认值: $post_type
labels
(array) (可选) 该新类型的数组参数。默认文章类型是 non-hierarchical type,页面类型是hierarchical ones。
默认:如果为空,则name为label的值,singular_name则为name的值

◆ ‘name’ – 新类型的名字, 可以使用$post_type_object->label重新赋值。
◆ ‘singular_name’ – 新类型的对象名. 默认是name的值。
◆ ‘add_new’ – ‘Add New’的翻译文字. 默认值是 ‘Add New’ . 在国际化这个字符串,请使用 gettext context匹配你的文章类型。例如: _x(‘Add New’, ‘product’);
◆ ‘add_new_item’ – 添加新项的文本. 默认值是 Add New Post/Add New Page
◆ ‘edit_item’ – 编辑项文本。 默认值是 Edit Post/Edit Page
◆ ‘new_item’ – 新的项目文本。 默认值是 New Post/New Page
◆ ‘view_item’ – 查看项目文本。 默认值是 View Post/View Page
◆ ‘search_items’ – 检索项的文本。默认值是 Search Posts/Search Pages
◆ ‘not_found’ – 未发现的文本。默认值是 No posts found/No pages found
◆ ‘not_found_in_trash’ – 未发现垃圾文本。默认值是 No posts found in Trash/No pages found in Trash。
◆ ‘parent_item_colon’ – 父文本。 该字符串不能使用到non-hierarchical 类型上 。在 hierarchical ones 默认值是 Parent Page。
◆ ‘menu_name’ – 菜单的文本。 这个字符串是给菜单项的名字。 默认值是 name 的值。
description
(string) (可选) 对新类型的简短描述.
Default: blank
public
(boolean) (可选) 为下列全部参数设置一个默认值: publicly_queriable, show_ui, show_in_nav_menus and exclude_from_search.
默认值: false
‘false’ – 不显示此类型的用户界面 (show_ui=false), 该类型无法从前端进行查询 (publicly_queryable=false), 在搜索结果中排除该类型文章 (exclude_from_search=true),在导航菜单中隐藏 (show_in_nav_menus=false)
‘true’ – show_ui=true, publicly_queryable=true, exclude_from_search=false, show_in_nav_menus=true
publicly_queryable
(boolean) (可选) 该类型查询允许从前端进行。
默认值: public的参数值
exclude_from_search
(boolean) (importance) 是否将该类型的文章从搜索结果中排除
默认值: public 参数的值
show_ui
(boolean) (可选) 是否使用默认的ui界面. Note that _built-in post types, such as post and page, are intentionally set to false.
默认值: public 参数的值
‘false’ – 不显示该类型的用户界面
‘true’ – 显示该类型的用户界面 (admin panel)
show_in_menu
(boolean or string) (可选) 是否显示该类型菜单,并设置显示的地方。 show_ui 必须为 true.
默认值: null
‘false’ – 不显示在管理菜单。
‘true’ – 作为顶级菜单显示。
‘some string’ – 作为顶级页面,像 ‘tools.php’ or ‘edit.php?post_type=page’
注意: 当使用’some string’为菜单页面通过插件创建顶级菜单项,该菜单项将会成为第一个菜单项。如果这不是你想要的结果。该插件创建菜单页面需要用add_action 将 admin_menu 设置为9或更低的优先级。
menu_position
(integer) (可选) 该菜单项的位置紧跟在哪个菜单项后面。
默认值: null – 默认位置为评论( Comments)后面

◆ 5 – below Posts(文章后面)
◆ 10 – below Media(多媒体后面)
◆ 15 – below Links(连接后面)
◆ 20 – below Pages(页面后面)
◆ 25 – below comments(评论后面)
◆ 60 – below first separator(第一个分隔后面)
◆ 65 – below Plugins(插件后面)
◆ 70 – below Users(用户后面)
◆ 75 – below Tools(工具后面)
◆ 80 – below Settings(设置后面)
◆ 100 – below second separator(第二个分隔后面)
menu_icon
(string) (可选) 菜单图标.
默认值: null – 默认使用文章图标
capability_type
(string or array) (可选) 该字符用于建立 read, edit, delete 功能。也可以通过传递数组来初始化功能,例如 array(‘story’, ‘stories’).
默认值: “post”
capabilities
(array) (可选) 该类型的功能数组.
默认值: capability_type
默认情况下该数组有7个键可以接受参数:

◆ edit_post, read_post, 和 delete_post – 这是三个元功能,通常根据上下文映射到相应的原始功能 。例如,文章被被编辑/读/删除前,先要对用户或角色进行权限检查。因此,这些功能通常不会直接授予用户或角色。
◆ edit_posts – 控制是否有多个此类型的对象被编辑。
◆ edit_others_posts – 控制此类型的文章可以被非所有者编辑.如果文章类型不支持作者,那么这将像edit_posts。
◆ publish_posts – 控件对象发布文章类型。
read_private_posts – 控制是否私有对象可以读。

注意: 后面的四项功能会在核心的不同位置检查。
也有其他七个原始的功能不是直接用在核心,但在map_meta_cap()。上述三个元功能,将它们转换成一个或多个原始的功能,就必须根据上下文对用户或角色的检查。这些额外的功能只用于map_meta_cap()。因此,如果该类型已经注册,“map_meta_cap” 默认值将为 true,否则默认为false。

read – 控制是否可以读取.

◆ delete_posts – 控制是否可以删除
◆ delete_private_posts – 控制是否可以删除私有的。
◆ delete_published_posts – 控制是否可以删除已发布的。
◆ delete_others_posts – 控制非所有者是否可以删除. 如果文章类型不支持作者,那么这将像delete_posts。
◆ edit_private_posts – 控制是否可以编辑私有的。
◆ edit_published_posts – 控制是否可以编辑已发布的。
map_meta_cap
(boolean) (可选) 是否使用内置默认的元功能。
默认值: false
hierarchical
(boolean) (可选) 是否此类型支持层级和允许被指定为父级

默认值: false
supports
(array) (可选) 调用 add_post_type_support() 的别名。
默认值: title 和 editor

◆ ‘title’
◆ ‘editor’ (content)
◆ ‘author’
◆ ‘thumbnail’ (featured image, current theme must also support post-thumbnails)
◆ ‘excerpt’
◆ ‘trackbacks’
◆ ‘custom-fields’
◆ ‘comments’ (also will see comment count balloon on edit screen)
◆ ‘revisions’ (will store revisions)
◆ ‘page-attributes’ (template and menu order, hierarchical must be true to show Parent option)
◆ ‘post-formats’ add post formats, see Post Formats
register_meta_box_cb
(string) (可选) 提供一个回调函数来控制页面。可以在该函数中使用 remove_meta_box() 和 add_meta_box() .
默认值: None
taxonomies
(array) (可选) 此类型支持的已注册分类法类型数组,如 category 或 post_tag 。 雷同register_taxonomy_for_object_type() 。分类法可以使用 register_taxonomy()来注册。

默认值: None
permalink_epmask
(string) (可选) The default rewrite endpoint bitmasks. For more info see Trac Ticket 12605.
Default: EP_PERMALINK
has_archive
(boolean or string) (可选) 如果允许该类型归档,将会使用字符串作为连接,如果支持url重写,该url将会被重写 。
默认值: false
rewrite
(boolean or array) (可选) url重写的规则. False 将不支持重写.
默认值: true 并使用类型作为连接
$args array

◆ ‘slug’ -默认使用此类型的 name , 自定义使用 array(‘slug’=>$slug)
◆ ‘with_front’ – 允许在连接上添加前缀 (例如:如果前缀是 /blog/, 你的连接: false->/news/, true->/blog/news/) – 默认为true
◆ ‘feeds’ – 默认是has_archive 的值
◆ ‘pages’ – 默认值 true
query_var
(boolean or string) (可选) False 不允许被查询, 否则该字符串值将会成为此类型的查询后的对象
默认: true – 设置 $post_type 为查询结果
can_export
(boolean) (可选) 此类型是否可以输出.
默认值: true
show_in_nav_menus
(boolean) (可选) post_type是否可在导航菜单选择。
Default: public 参数的值
_builtin
(boolean) (not for general use) Whether this post type is a native or “built-in” post_type. Note: this Codex entry is for documentation – core developers recommend you don’t use this when registering your own post type
Default: false
‘false’ – default this is a custom post type
‘true’ – this is a built-in native post type (post, page, attachment, revision, nav_menu_item)
_edit_link
(boolean) (not for general use) Link to edit an entry with this post type. Note: this Codex entry is for documentation ‘-‘ core developers recommend you don’t use this when registering your own post type
Default:
‘post.php?post=%d’

范例

注册一个文章类型叫 “book”的范例,包括提供上下文帮助:

<?php
add_action('init', 'my_custom_init');
function my_custom_init()
{
  $labels = array(
    'name' => _x('Books', 'post type general name'),
    'singular_name' => _x('Book', 'post type singular name'),
    'add_new' => _x('Add New', 'book'),
    'add_new_item' => __('Add New Book'),
    'edit_item' => __('Edit Book'),
    'new_item' => __('New Book'),
    'view_item' => __('View Book'),
    'search_items' => __('Search Books'),
    'not_found' =>  __('No books found'),
    'not_found_in_trash' => __('No books found in Trash'),
    'parent_item_colon' => '',
    'menu_name' => 'Books'
  );

  $args = array(
    'labels' => $labels,
    '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' => null,
    'supports' =>array('title','editor','author','thumbnail','excerpt','comments')
  );

  register_post_type('book',$args);
}

//添加过滤器,以确保课本用户更新时会显示。

add_filter('post_updated_messages', 'book_updated_messages');
function book_updated_messages( $messages ) {
  global $post, $post_ID;
  $messages['book'] = array(
    0 => '', // 未使用。信息开始在索引1。
    1 => sprintf( __('Book updated. <a href="%s">View book</a>'), esc_url( get_permalink($post_ID) ) ),
    2 => __('Custom field updated.'),
    3 => __('Custom field deleted.'),
    4 => __('Book updated.'),
    /* translators: %s: date and time of the revision */
    5 => isset($_GET['revision']) ? sprintf( __('Book restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
    6 => sprintf( __('Book published. <a href="%s">View book</a>'), esc_url( get_permalink($post_ID) ) ),
    7 => __('Book saved.'),
    8 => sprintf( __('Book submitted. <a target="_blank" href="%s">Preview book</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
    9 => sprintf( __('Book scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview book</a>'),
      // translators: Publish box date format, see http://php.net/date
      date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
    10 => sprintf( __('Book draft updated. <a target="_blank" href="%s">Preview book</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
  );
  return $messages;
}

//显示图书上下文帮助

add_action( 'contextual_help', 'add_help_text', 10, 3 );
function add_help_text($contextual_help, $screen_id, $screen) {
  //$contextual_help .= var_dump($screen); // use this to help determine $screen->id
  if ('book' == $screen->id ) {
    $contextual_help =
      '<p>' . __('Things to remember when adding or editing a book:') . '</p>' .
      '<ul>' .
      '<li>' . __('Specify the correct genre such as Mystery, or Historic.') . '</li>' .
      '<li>' . __('Specify the correct writer of the book.  Remember that the Author module refers to you, the author of this book review.') . '</li>' .
      '</ul>' .
      '<p>' . __('If you want to schedule the book review to be published in the future:') . '</p>' .
      '<ul>' .
      '<li>' . __('Under the Publish module, click on the Edit link next to Publish.') . '</li>' .
      '<li>' . __('Change the date to the date to actual publish this article, then click on Ok.') . '</li>' .
      '</ul>' .
      '<p><strong>' . __('For more information:') . '</strong></p>' .
      '<p>' . __('<a href="http://codex.wordpress.org/Posts_Edit_SubPanel" target="_blank">Edit Posts Documentation</a>') . '</p>' .
      '<p>' . __('<a href="http://wordpress.org/support/" target="_blank">Support Forums</a>') . '</p>' ;
  } elseif ( 'edit-book' == $screen->id ) {
    $contextual_help =
      '<p>' . __('This is the help screen displaying the table of books blah blah blah.') . '</p>' ;
  }
  return $contextual_help;
}

开始版本
WP 2.9

源文件

register_post_type() is located in wp-includes/post.php.

官方文档

http://codex.wordpress.org/zh-cn:函数参考/register_post_type

链接到文章: https://jiloc.com/43831.html

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注