使用wordpress注册页面类型register_post_type(增长一个 产品中心、案例 之类的 与文章同级的post-type)


<?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);  
  
  //为book添加分类  
  register_taxonomy('genre',array('book'), array(  
    'hierarchical' => true,//true为分类,false为标签  
  ));  
}  
  
//添加过滤器,以确保课本用户更新时会显示。  
  
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;  
}  
?>



说明

建立或修改一个文章类型。该函数不能在init动做以前使用。php

用法html

参数前端

$post_type

(string) (必选) 文章类型。(最多20个字符)数组

默认值: 空编辑器

$args
( array) ( 可选) 一个数组参数。
默认值: 空

$args参数说明

label

(string) (可选)    是新类型的别名标记ide

默认值: $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个键能够接受参数:wordpress

  • edit_postread_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
  • *           'title' 标题
    * 'editor' (content) 编辑器* 'author' 做者* 'thumbnail' 特点图片* 'excerpt' 摘抄* 'trackbacks' 引用经过* 'custom-fields' 自定义字段* 'comments' 评论* 'revisions' 修订-将修改存储* 'page-attributes' 菜单顺序* '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()来注册。post

默认值:  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"的范例,包括提供上下文帮助:


 原文地址:http://www.xggxgg.com/392.html