register_taxonomy()

最后更新于:2021-11-27 21:38:14

register_taxonomy( string$taxonomy, array|string$object_type, array|string$args=array())

Creates or modifies a taxonomy object.

参数

$taxonomy

(string) (Required) Taxonomy key, must not exceed 32 characters.

$object_type

(array|string) (Required) Object type or array of object types with which the taxonomy should be associated.

$args

(array|string) (Optional) Array or query string of arguments for registering a taxonomy.

  • ‘labels’
    (string[]) An array of labels for this taxonomy. By default, Tag labels are used for non-hierarchical taxonomies, and Category labels are used for hierarchical taxonomies. See accepted values in get_taxonomy_labels().
  • ‘description’
    (string) A short descriptive summary of what the taxonomy is for.
  • ‘public’
    (bool) Whether a taxonomy is intended for use publicly either via the admin interface or by front-end users. The default settings of $publicly_queryable, $show_ui, and $show_in_nav_menus are inherited from $public.
  • ‘publicly_queryable’
    (bool) Whether the taxonomy is publicly queryable. If not set, the default is inherited from $public
  • ‘hierarchical’
    (bool) Whether the taxonomy is hierarchical. Default false.
  • ‘show_ui’
    (bool) Whether to generate and allow a UI for managing terms in this taxonomy in the admin. If not set, the default is inherited from $public (default true).
  • ‘show_in_menu’
    (bool) Whether to show the taxonomy in the admin menu. If true, the taxonomy is shown as a submenu of the object type menu. If false, no menu is shown. $show_ui must be true. If not set, default is inherited from $show_ui (default true).
  • ‘show_in_nav_menus’
    (bool) Makes this taxonomy available for selection in navigation menus. If not set, the default is inherited from $public (default true).
  • ‘show_in_rest’
    (bool) Whether to include the taxonomy in the REST API. Set this to true for the taxonomy to be available in the block editor.
  • ‘rest_base’
    (string) To change the base url of REST API route. Default is $taxonomy.
  • ‘rest_controller_class’
    (string) REST API Controller class name. Default is ‘GC_REST_Terms_Controller‘.
  • ‘show_tagcloud’
    (bool) Whether to list the taxonomy in the Tag Cloud Widget controls. If not set, the default is inherited from $show_ui (default true).
  • ‘show_in_quick_edit’
    (bool) Whether to show the taxonomy in the quick/bulk edit panel. It not set, the default is inherited from $show_ui (default true).
  • ‘show_admin_column’
    (bool) Whether to display a column for the taxonomy on its post type listing screens. Default false.
  • ‘meta_box_cb’
    (bool|callable) Provide a callback function for the meta box display. If not set, post_categories_meta_box() is used for hierarchical taxonomies, and post_tags_meta_box() is used for non-hierarchical. If false, no meta box is shown.
  • ‘meta_box_sanitize_cb’
    (callable) Callback function for sanitizing taxonomy data saved from a meta box. If no callback is defined, an appropriate one is determined based on the value of $meta_box_cb.
  • ‘capabilities’
    (string[]) Array of capabilities for this taxonomy.
    • ‘manage_terms’
      (string) Default ‘manage_categories’.
    • ‘edit_terms’
      (string) Default ‘manage_categories’.
    • ‘delete_terms’
      (string) Default ‘manage_categories’.
    • ‘assign_terms’
      (string) Default ‘edit_posts’.
  • ‘rewrite’
    (bool|array) Triggers the handling of rewrites for this taxonomy. Default true, using $taxonomy as slug. To prevent rewrite, set to false. To specify rewrite rules, an array can be passed with any of these keys:
    • ‘slug’
      (string) Customize the permastruct slug. Default $taxonomy key.
    • ‘with_front’
      (bool) Should the permastruct be prepended with GC_Rewrite::$front. Default true.
    • ‘hierarchical’
      (bool) Either hierarchical rewrite tag or not. Default false.
    • ‘ep_mask’
      (int) Assign an endpoint mask. Default EP_NONE.
  • ‘query_var’
    (string|bool) Sets the query var key for this taxonomy. Default $taxonomy key. If false, a taxonomy cannot be loaded at ?{query_var}={term_slug}. If a string, the query ?{query_var}={term_slug} will be valid.
  • ‘update_count_callback’
    (callable) Works much like a hook, in that it will be called when the count is updated. Default _update_post_term_count() for taxonomies attached to post types, which confirms that the objects are published before counting them. Default _update_generic_term_count() for taxonomies attached to other object types, such as users.
  • ‘default_term’
    (string|array) Default term to be used for the taxonomy.
    • ‘name’
      (string) Name of default term.
    • ‘slug’
      (string) Slug for default term.
    • ‘description’
      (string) Description for default term.
  • ‘sort’
    (bool) Whether terms in this taxonomy should be sorted in the order they are provided to gc_set_object_terms(). Default null which equates to false.
  • ‘args’
    (array) Array of arguments to automatically use inside gc_get_object_terms() for this taxonomy.
  • ‘_builtin’
    (bool) This taxonomy is a “built-in” taxonomy. INTERNAL USE ONLY! Default false.

Default value: array()

响应

(GC_Taxonomy|GC_Error) The registered taxonomy object on success, GC_Error object on failure.

源文件

文件: gc-includes/taxonomy.php

function register_taxonomy( $taxonomy, $object_type, $args = array() ) {
	global $gc_taxonomies;

	if ( ! is_array( $gc_taxonomies ) ) {
		$gc_taxonomies = array();
	}

	$args = gc_parse_args( $args );

	if ( empty( $taxonomy ) || strlen( $taxonomy ) > 32 ) {
		_doing_it_wrong( __FUNCTION__, __( 'Taxonomy names must be between 1 and 32 characters in length.' ), '4.2.0' );
		return new GC_Error( 'taxonomy_length_invalid', __( 'Taxonomy names must be between 1 and 32 characters in length.' ) );
	}

	$taxonomy_object = new GC_Taxonomy( $taxonomy, $object_type, $args );
	$taxonomy_object->add_rewrite_rules();

	$gc_taxonomies[ $taxonomy ] = $taxonomy_object;

	$taxonomy_object->add_hooks();

	// Add default term.
	if ( ! empty( $taxonomy_object->default_term ) ) {
		$term = term_exists( $taxonomy_object->default_term['name'], $taxonomy );
		if ( $term ) {
			update_option( 'default_term_' . $taxonomy_object->name, $term['term_id'] );
		} else {
			$term = gc_insert_term(
				$taxonomy_object->default_term['name'],
				$taxonomy,
				array(
					'slug'        => sanitize_title( $taxonomy_object->default_term['slug'] ),
					'description' => $taxonomy_object->default_term['description'],
				)
			);

			// Update `term_id` in options.
			if ( ! is_gc_error( $term ) ) {
				update_option( 'default_term_' . $taxonomy_object->name, $term['term_id'] );
			}
		}
	}

	/**
	 * Fires after a taxonomy is registered.
	 *
	 * @since 3.3.0
	 *
	 * @param string       $taxonomy    Taxonomy slug.
	 * @param array|string $object_type Object type or array of object types.
	 * @param array        $args        Array of taxonomy registration arguments.
	 */
	do_action( 'registered_taxonomy', $taxonomy, $object_type, (array) $taxonomy_object );

	return $taxonomy_object;
}
/**
 * Create two taxonomies, genres and writers for the post type "book".
 *
 * @see register_post_type() for registering custom post types.
 */
function gcdocs_create_book_taxonomies() {
	// Add new taxonomy, make it hierarchical (like categories)
	$labels = array(
		'name'              => _x( 'Genres', 'taxonomy general name', 'textdomain' ),
		'singular_name'     => _x( 'Genre', 'taxonomy singular name', 'textdomain' ),
		'search_items'      => __( 'Search Genres', 'textdomain' ),
		'all_items'         => __( 'All Genres', 'textdomain' ),
		'parent_item'       => __( 'Parent Genre', 'textdomain' ),
		'parent_item_colon' => __( 'Parent Genre:', 'textdomain' ),
		'edit_item'         => __( 'Edit Genre', 'textdomain' ),
		'update_item'       => __( 'Update Genre', 'textdomain' ),
		'add_new_item'      => __( 'Add New Genre', 'textdomain' ),
		'new_item_name'     => __( 'New Genre Name', 'textdomain' ),
		'menu_name'         => __( 'Genre', 'textdomain' ),
	);

	$args = array(
		'hierarchical'      => true,
		'labels'            => $labels,
		'show_ui'           => true,
		'show_admin_column' => true,
		'query_var'         => true,
		'rewrite'           => array( 'slug' => 'genre' ),
	);

	register_taxonomy( 'genre', array( 'book' ), $args );

	unset( $args );
	unset( $labels );

	// Add new taxonomy, NOT hierarchical (like tags)
	$labels = array(
		'name'                       => _x( 'Writers', 'taxonomy general name', 'textdomain' ),
		'singular_name'              => _x( 'Writer', 'taxonomy singular name', 'textdomain' ),
		'search_items'               => __( 'Search Writers', 'textdomain' ),
		'popular_items'              => __( 'Popular Writers', 'textdomain' ),
		'all_items'                  => __( 'All Writers', 'textdomain' ),
		'parent_item'                => null,
		'parent_item_colon'          => null,
		'edit_item'                  => __( 'Edit Writer', 'textdomain' ),
		'update_item'                => __( 'Update Writer', 'textdomain' ),
		'add_new_item'               => __( 'Add New Writer', 'textdomain' ),
		'new_item_name'              => __( 'New Writer Name', 'textdomain' ),
		'separate_items_with_commas' => __( 'Separate writers with commas', 'textdomain' ),
		'add_or_remove_items'        => __( 'Add or remove writers', 'textdomain' ),
		'choose_from_most_used'      => __( 'Choose from the most used writers', 'textdomain' ),
		'not_found'                  => __( 'No writers found.', 'textdomain' ),
		'menu_name'                  => __( 'Writers', 'textdomain' ),
	);

	$args = array(
		'hierarchical'          => false,
		'labels'                => $labels,
		'show_ui'               => true,
		'show_admin_column'     => true,
		'update_count_callback' => '_update_post_term_count',
		'query_var'             => true,
		'rewrite'               => array( 'slug' => 'writer' ),
	);

	register_taxonomy( 'writer', 'book', $args );
}
// hook into the init action and call create_book_taxonomies when it fires
add_action( 'init', 'gcdocs_create_book_taxonomies', 0 );
/**
 * Register a private 'Genre' taxonomy for post type 'book'.
 *
 * @see register_post_type() for registering post types.
 */
function gcdocs_register_private_taxonomy() {
 	$args = array(
		'label'        => __( 'Genre', 'textdomain' ),
		'public'       => false,
		'rewrite'      => false,
		'hierarchical' => true
	);
	
	register_taxonomy( 'genre', 'book', $args );
}
add_action( 'init', 'gcdocs_register_private_taxonomy', 0 );
/**
 * Register a 'genre' taxonomy for post type 'book'.
 *
 * Register custom capabilities for taxonomies.
 *
 * @see register_post_type for registering post types.
 */
function gcdocs_create_book_tax() {
    register_taxonomy( 'genre', 'book', array(
        'label'        => __( 'Genre', 'textdomain' ),
        'rewrite'      => array( 'slug' => 'genre' ),
        'hierarchical' => true,
		'capabilities' => array(
			// $taxonomy['slug'] = genre;
			'manage_terms'	=>	'manage_'.$taxonomy['slug'],
			'edit_terms'	=>	'edit_'.$taxonomy['slug'],
			'delete_terms'	=>	'delete'.$taxonomy['slug'],
			'assign_terms'	=>	'assign_'.$taxonomy['slug'],
		),
    ) );
}
add_action( 'init', 'gcdocs_create_book_tax', 0 );
function register_custom_taxonomy() {

	$labels = array(
		'name'              => _x( 'Genres', 'taxonomy general name', 'textdomain' ),
		'singular_name'     => _x( 'Genre', 'taxonomy singular name', 'textdomain' ),
		'search_items'      => __( 'Search Genres', 'textdomain' ),
		'all_items'         => __( 'All Genres', 'textdomain' ),
		'view_item'         => __( 'View Genre', 'textdomain' ),
		'parent_item'       => __( 'Parent Genre', 'textdomain' ),
		'parent_item_colon' => __( 'Parent Genre:', 'textdomain' ),
		'edit_item'         => __( 'Edit Genre', 'textdomain' ),
		'update_item'       => __( 'Update Genre', 'textdomain' ),
		'add_new_item'      => __( 'Add New Genre', 'textdomain' ),
		'new_item_name'     => __( 'New Genre Name', 'textdomain' ),
		'not_found'         => __( 'No Genres Found', 'textdomain' ),
		'back_to_items'     => __( 'Back to Genres', 'textdomain' ),
		'menu_name'         => __( 'Genre', 'textdomain' ),
	);

	$args = array(
		'labels'            => $labels,
		'hierarchical'      => true,
		'public'            => true,
		'show_ui'           => true,
		'show_admin_column' => true,
		'query_var'         => true,
		'rewrite'           => array( 'slug' => 'genre' ),
		'show_in_rest'      => true,
	);


	register_taxonomy( 'genre', 'book', $args );

}
function add_post_taxonomy() {

  $taxArray = array(
    array(
      "taxName" => 'tax中文名1',
      "taxNameEn" =>'taxSlu1'
    ),
    array(
      "taxName" => 'tax中文名2',
      "taxNameEn" =>'taxSlu2'
    ),
  );

  foreach ($taxArray as $tax) {
    $labels = array(
      "name" => __( "", "" ),
      "singular_name" => __( $tax['taxName'], "" ),
      "menu_name" => __( $tax['taxName'], "" ),
      "all_items" => __( "所有", "" ),
      "edit_item" => __( "編輯", "" ),
      "view_item" => __( "檢視", "" ),
      "update_item" => __( "更新", "" ),
      "add_new_item" => __( "新增", "" ),
      "new_item_name" => __( "新增", "" ),
      "search_items" => __( "搜尋", "" ),
    );

    $args = array(
      "label" => __( $tax['taxName'], "" ),
      "labels" => $labels,
      "public" => true,
      "hierarchical" => true,
      "label" => $tax['taxName'],
      "show_ui" => true,
      "show_in_menu" => true,
      "show_in_nav_menus" => true,
      "show_admin_column" => true,
      "query_var" => true,
      "rewrite" => array( 'slug' => $tax['taxNameEn'], 'with_front' => true, ),
      "show_admin_column" => true,
      "show_in_rest" => false,
      "rest_base" => $tax['taxNameEn'],
      "show_in_quick_edit" => true,
    );
    register_taxonomy( $tax['taxNameEn'], 'post', $args );
  }
  
}
add_action( 'init', 'add_post_taxonomy' );
/**
 * Register a 'genre' taxonomy for post type 'book'.
 *
 * @see register_post_type for registering post types.
 */
function gcdocs_create_book_tax() {
	register_taxonomy( 'genre', 'book', array(
		'label'        => __( 'Genre', 'textdomain' ),
		'rewrite'      => array( 'slug' => 'genre' ),
		'hierarchical' => true,
	) );
}
add_action( 'init', 'gcdocs_create_book_tax', 0 );