register_taxonomy()注册一个分类法

目录

描述

译文

register_taxonomy()函数可添加或改写分类法。函数包括它所影响到的一个名称,一个对象,以及一个参数数组。函数不返回任何值。

原文

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. In particular, capital letters should be avoided (This was allowed in 3.0, but not enforced until 3.1 with the "Cheatin'" error).

register_taxonomy() 描述

用法

 <?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.

Better be safe than sorry when registering custom taxonomies for custom post types. Use register_taxonomy_for_object_type() right after the function to interconnect them. Else you could run into minetraps where the post type isn't attached inside filter callback that run during parse_request or pre_get_posts.

register_taxonomy() 用法

参数

$taxonomy

(string) (必填) The name of the taxonomy. Name should only contain lowercase letters and the underscore character, and not be more than 32 characters long (database structure restriction).

默认值: None

$object_type

(array/string) (必填) Name of the object type for the taxonomy object. Object-types can be built-in Post Type or any Custom Post Type that may be registered.

默认值: None

Built-in Post Types:

  • post
  • page
  • attachment
  • revision
  • nav_menu_item

Custom Post Types:

  • {custom_post_type} - Custom Post Type names must be all in lower-case and without any spaces.
  • null - Setting explicitly to null registers the taxonomy but doesn't associate it with any objects, so it won't be directly available within the Admin UI. You will need to manually register it using the 'taxonomy' parameter (passed through $args) when registering a custom post_type (see register_post_type()), or using register_taxonomy_for_object_type().

$args

(array/string) (可选) An array of Arguments.

默认值: None

Arguments

label

(string) (可选) A plural descriptive name for the taxonomy marked for translation.

默认值: overridden by $labels->name

labels

(array) (可选) labels - An array of labels for this taxonomy. By default tag labels are used for non-hierarchical types and category labels for hierarchical ones.

默认值: 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');
  • 'menu_name' - the menu name text. This string is the name to give menu items. If not set, defaults to value of name label.
  • 'all_items' - the all items text. Default is __( 'All Tags' ) or __( 'All Categories' )
  • 'edit_item' - the edit item text. Default is __( 'Edit Tag' ) or __( 'Edit Category' )
  • 'view_item' - the view item text, Default is __( 'View Tag' ) or __( 'View 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' )
  • '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:' )
  • 'search_items' - the search items text. Default is __( 'Search Tags' ) or __( 'Search Categories' )
  • 'popular_items' - the popular items text. This string is not used on hierarchical taxonomies. Default is __( 'Popular Tags' ) or null
  • 'separate_items_with_commas' - the separate item with commas text used in the taxonomy meta box. This string is not 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 is not 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 is not used on hierarchical taxonomies. Default is __( 'Choose from the most used tags' ) or null
  • 'not_found' (3.6+) - the text displayed via clicking 'Choose from the most used tags' in the taxonomy meta box when no tags are available and (4.2+) - the text used in the terms list table when there are no items for a taxonomy. Default is __( 'No tags found.' ) or __( 'No categories found.' )

public

(boolean) (可选) If the taxonomy should be publicly queryable.

默认值: true

show_ui

(boolean) (可选) Whether to generate a default UI for managing this taxonomy.

默认值: if not set, defaults to value of public argument. As of 3.5, setting this to false for attachment taxonomies will hide the UI.

show_in_nav_menus

(boolean) (可选) true makes this taxonomy available for selection in navigation menus.

默认值: if not set, defaults to value of public argument

show_tagcloud

(boolean) (可选) Whether to allow the Tag Cloud widget to use this taxonomy.

默认值: if not set, defaults to value of show_ui argument

show_in_quick_edit

(boolean) (可选) Whether to show the taxonomy in the quick/bulk edit panel. (Available since 4.2)

默认值: if not set, defaults to value of show_ui argument

meta_box_cb

(callback) (可选) Provide a callback function name for the meta box display. (Available since 3.8)

默认值: null

Note: Defaults to the categories meta box (post_categories_meta_box() in meta-boxes.php) for hierarchical taxonomies and the tags meta box (post_tags_meta_box()) for non-hierarchical taxonomies. No meta box is shown if set to false.

show_admin_column

(boolean) (可选) Whether to allow automatic creation of taxonomy columns on associated post-types table. (Available since 3.5)

默认值: false

description

(string) (可选) Include a description of the taxonomy.

默认值: ""

hierarchical

(boolean) (可选) Is this taxonomy hierarchical (have descendants) like categories or not hierarchical like tags.

默认值: false

Note: Hierarchical taxonomies will have a list with checkboxes to select an existing category in the taxonomy admin box on the post edit page (like default post categories). Non-hierarchical taxonomies will just have an empty text field to type-in taxonomy terms to associate with the post (like default post tags).

update_count_callback

(string) (可选) A function name that will be called when the count of an associated $object_type, such as post, is updated. Works much like a hook.

默认值: None - but see Note, below.

Note: While the default is '', when actually performing the count update in wp_update_term_count_now(), if the taxonomy is only attached to post types (as opposed to other WordPress objects, like user), the built-in _update_post_term_count() function will be used to count only published posts associated with that term, otherwise _update_generic_term_count() will be used instead, that does no such checking.

This is significant in the case of attachments. Because an attachment is a type of post, the default _update_post_term_count() will be used. However, this may be undesirable, because this will only count attachments that are actually attached to another post (like when you insert an image into a post). This means that attachments that you simply upload to WordPress using the Media Library, but do not actually attach to another post will not be counted. If your intention behind associating a taxonomy with attachments was to leverage the Media Library as a sort of Document Management solution, you are probably more interested in the counts of unattached Media items, than in those attached to posts. In this case, you should force the use of _update_generic_term_count() by setting '_update_generic_term_count' as the value for update_count_callback.

query_var

(boolean or string) (可选) False to disable the query_var, set as string to use custom query_var instead of default which is $taxonomy, the taxonomy's "name".

默认值: $taxonomy

Note: The query_var is used for direct queries through WP_Query like new WP_Query(array('people'=>$person_name)) and URL queries like /?people=$person_name. Setting query_var to false will disable these methods, but you can still fetch posts with an explicit WP_Query taxonomy query like WP_Query(array('taxonomy'=>'people', 'term'=>$person_name)).

rewrite

(boolean/array) (可选) Set to false to prevent automatic URL rewriting a.k.a. "pretty permalinks". Pass an $args array to override default URL settings for permalinks as outlined below:

默认值: true

  • 'slug' - Used as pretty permalink text (i.e. /tag/) - defaults to $taxonomy (taxonomy's name slug)
  • '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) - defaults to false
  • 'ep_mask' - (Required for pretty permalinks) Assign an endpoint mask for this taxonomy - defaults to EP_NONE. If you do not specify the EP_MASK, pretty permalinks will not work. For more info see this Make WordPress Plugins summary of endpoints.

Note: You may need to flush the rewrite rules after changing this. You can do it manually by going to the Permalink Settings page and re-saving the rules -- you don't need to change them -- or by calling $wp_rewrite->flush_rules(). You should only flush the rules once after the taxonomy has been created, not every time the plugin/theme loads.

capabilities

(array) (可选) An array of the capabilities for this taxonomy.

默认值: None

  • 'manage_terms' - 'manage_categories'
  • 'edit_terms' - 'manage_categories'
  • 'delete_terms' - 'manage_categories'
  • 'assign_terms' - 'edit_posts'

sort

(boolean) (可选) Whether this taxonomy should remember the order in which terms are added to objects.

默认值: None

_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

默认值: false

register_taxonomy() 参数

源文件

register_taxonomy() 函数的代码位于 wp-includes/taxonomy.php.

register_taxonomy() 源文件

相关