zh-cn:函数参考/register taxonomy
Languages:
English •
中文(简体) •
日本語 •
(Add your language)
说明
This function adds or overwrites a taxonomy. It takes in a name, an object name that it affects, and an array of parameters. It does not return anything.
Care should be used in selecting a taxonomy name so that it does not conflict with other taxonomies, post types, and reserved WordPress public and private query variables. A complete list of those is described in the Reserved Terms section.
用法
<?php register_taxonomy($taxonomy, $object_type, $args); ?>
Use the init action to call this function. Calling it outside of an action can lead to troubles. See #15568 for details.
参数
- $taxonomy
- (string) (required) The name of the taxonomy.
- Default: None
- $object_type
- (array/string) (required) Name of the object type for the taxonomy object.
- Default: None
- $args
- (array/string) (optional) An array of Arguments.
- Default: None
Arguments
- label
- (string) (optional) A plural descriptive name for the taxonomy marked for translation.
- Default: overridden by $labels->name
- labels
- (array) (optional) labels - An array of labels for this taxonomy. By default tag labels are used for non-hierarchical types and category labels for hierarchical ones.
- Default: if empty, name is set to label value, and singular_name is set to name value
- 'name' - general name for the taxonomy, usually plural. The same as and overridden by $tax->label. Default is _x( 'Post Tags', 'taxonomy general name' ) or _x( 'Categories', 'taxonomy general name' ). When internationalizing this string, please use a gettext context matching your post type. Example:
_x('Writers', 'taxonomy general name');
- 'singular_name' - name for one object of this taxonomy. Default is _x( 'Post Tag', 'taxonomy singular name' ) or _x( 'Category', 'taxonomy singular name' ). When internationalizing this string, please use a gettext context matching your post type. Example:
_x('Writer', 'taxonomy singular name');
- 'search_items' - the search items text. Default is __( 'Search Tags' ) or __( 'Search Categories' )
- 'popular_items' - the popular items text. Default is __( 'Popular Tags' ) or null
- 'all_items' - the all items text. Default is __( 'All Tags' ) or __( 'All Categories' )
- 'parent_item' - the parent item text. This string is not used on non-hierarchical taxonomies such as post tags. Default is null or __( 'Parent Category' )
- 'parent_item_colon' - The same as
parent_item, but with colon : in the end null, __( 'Parent Category:' )
- 'edit_item' - the edit item text. Default is __( 'Edit Tag' ) or __( 'Edit Category' )
- 'update_item' - the update item text. Default is __( 'Update Tag' ) or __( 'Update Category' )
- 'add_new_item' - the add new item text. Default is __( 'Add New Tag' ) or __( 'Add New Category' )
- 'new_item_name' - the new item name text. Default is __( 'New Tag Name' ) or __( 'New Category Name' )
- 'separate_items_with_commas' - the separate item with commas text used in the taxonomy meta box. This string isn't used on hierarchical taxonomies. Default is __( 'Separate tags with commas' ), or null
- 'add_or_remove_items' - the add or remove items text and used in the meta box when JavaScript is disabled. This string isn't used on hierarchical taxonomies. Default is __( 'Add or remove tags' ) or null
- 'choose_from_most_used' - the choose from most used text used in the taxonomy meta box. This string isn't used on hierarchical taxonomies. Default is __( 'Choose from the most used tags' ) or null
- 'menu_name' - the menu name text. This string is the name to give menu items. Defaults to value of name
- public
- (boolean) (optional) Should this taxonomy be exposed in the admin UI.
- Default: true
- show_in_nav_menus
- (boolean) (optional) true makes this taxonomy available for selection in navigation menus.
- Default: if not set, defaults to value of public argument
- show_ui
- (boolean) (optional) Whether to generate a default UI for managing this taxonomy.
- Default: if not set, defaults to value of public argument
- show_tagcloud
- (boolean) (optional) Wether to allow the Tag Cloud widget to use this taxonomy.
- Default: if not set, defaults to value of show_ui argument
- hierarchical
- (boolean) (optional) Is this taxonomy hierarchical (have descendants) like categories or not hierarchical like tags.
- Default: false
- update_count_callback
- (string) (optional) A function name that will be called to update the count of an associated $object_type, such as post, is updated.
- Default: None
- rewrite
- (boolean or array) (optional) Set to false to prevent rewrite, or array to customize customize query var. Default will use $taxonomy as query var
- Default: true
- $args array
- 'slug' - prepend posts with this slug - defaults to taxonomy's name
- 'with_front' - allowing permalinks to be prepended with front base - defaults to true
- 'hierarchical' - true or false allow hierarchical urls (implemented in Version 3.1)
- query_var
- (boolean or string) (optional) False to prevent queries, or string to customize query var. Default will use $taxonomy as query var
- Default: $taxonomy
- capabilities
- (array) (optional) An array of the capabilities for this taxonomy.
- Default: None
- 'manage_terms' - 'manage_categories'
- 'edit_terms' - 'manage_categories'
- 'delete_terms' - 'manage_categories'
- 'assign_terms' - 'edit_posts'
- _builtin
- (boolean) (not for general use) Whether this taxonomy is a native or "built-in" taxonomy. Note: this Codex entry is for documentation - core developers recommend you don't use this when registering your own taxonomy
- Default: false
范例
An an example of registering a two taxonomies, genres and writers, for the post type called "book" (uses Version 3.1 arguments):
Note: You can define custom taxonomies in a themes's functions.php template file:
<?php
//hook into the init action and call create_book_taxonomies when it fires
add_action( 'init', 'create_book_taxonomies', 0 );
//create two taxonomies, genres and writers for the post type "book"
function create_book_taxonomies()
{
// Add new taxonomy, make it hierarchical (like categories)
$labels = array(
'name' => _x( 'Genres', 'taxonomy general name' ),
'singular_name' => _x( 'Genre', 'taxonomy singular name' ),
'search_items' => __( 'Search Genres' ),
'all_items' => __( 'All Genres' ),
'parent_item' => __( 'Parent Genre' ),
'parent_item_colon' => __( 'Parent Genre:' ),
'edit_item' => __( 'Edit Genre' ),
'update_item' => __( 'Update Genre' ),
'add_new_item' => __( 'Add New Genre' ),
'new_item_name' => __( 'New Genre Name' ),
'menu_name' => __( 'Genre' ),
);
register_taxonomy('genre',array('book'), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'genre' ),
));
// Add new taxonomy, NOT hierarchical (like tags)
$labels = array(
'name' => _x( 'Writers', 'taxonomy general name' ),
'singular_name' => _x( 'Writer', 'taxonomy singular name' ),
'search_items' => __( 'Search Writers' ),
'popular_items' => __( 'Popular Writers' ),
'all_items' => __( 'All Writers' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Writer' ),
'update_item' => __( 'Update Writer' ),
'add_new_item' => __( 'Add New Writer' ),
'new_item_name' => __( 'New Writer Name' ),
'separate_items_with_commas' => __( 'Separate writers with commas' ),
'add_or_remove_items' => __( 'Add or remove writers' ),
'choose_from_most_used' => __( 'Choose from the most used writers' ),
'menu_name' => __( 'Writers' ),
);
register_taxonomy('writer','book',array(
'hierarchical' => false,
'labels' => $labels,
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'writer' ),
));
}
?>
Reserved Terms
- attachment
- attachment_id
- author
- author_name
- calendar
- cat
- category__and
- category__in
- category__not_in
- category_name
- comments_per_page
- comments_popup
- cpage
- day
- debug
- error
- exact
- feed
- hour
- link
- minute
- monthnum
- more
- name
- nav_menu
- nopaging
- offset
- order
- orderby
- p
- page
- page_id
- paged
- pagename
- pb
- perm
- post
- post_format
- post_mime_type
- post_status
- post_type
- post_type
- posts
- posts_per_archive_page
- posts_per_page
- preview
- robots
- s
- search
- second
- sentence
- showposts
- static
- subpost
- subpost_id
- tag
- tag__and
- tag__in
- tag__not_in
- tag_id
- tag_slug__and
- tag_slug__in
- taxonomy
- tb
- term
- type
- w
- withcomments
- withoutcomments
- year
更改日志
Since: 2.3
源文件
register_taxonomy() is located in wp-includes/taxonomy.php.
相关