get_taxonomy_labels()WP 3.0.0

Builds an object with all taxonomy labels out of a taxonomy object.

Return

Object. Taxonomy labels object. The first default value is for non-hierarchical taxonomies (like tags) and the second one is for hierarchical taxonomies (like categories).

Usage

get_taxonomy_labels( $tax );
$tax(WP_Taxonomy) (required)
Taxonomy object.

Examples

0

#1 Demo

$tax = get_taxonomy( 'category' );

$labels = get_taxonomy_labels( $tax );

print_r( $labels );

/* We get:
stdClass Object
(
	[name] => Categories
	[singular_name] => Category
	[search_items] => Search categories
	[popular_items] =>
	[all_items] => All categories
	[parent_item] => Parent category
	[parent_item_colon] => Parent category:
	[edit_item] => Change categories
	[view_item] => View categories
	[update_item] => Update categories
	[add_new_item] => Add new categories
	[new_item_name] => New category name
	[separate_items_with_commas] =>
	[add_or_remove_items] =>
	[choose_from_most_used] =>
	[not_found] => No categories found.
	[no_terms] => No categories
	[menu_name] => Categories                   // defaults to `name` property. Used for the tab in the admin menu.
	[name_admin_bar] => category
)
*/

Changelog

Since 3.0.0 Introduced.
Since 4.3.0 Added the no_terms label.
Since 4.4.0 Added the items_list_navigation and items_list labels.
Since 4.9.0 Added the most_used and back_to_items labels.
Since 5.7.0 Added the filter_by_item label.
Since 5.8.0 Added the item_link and item_link_description labels.
Since 5.9.0 Added the name_field_description, slug_field_description, parent_field_description, and desc_field_description labels.

get_taxonomy_labels() code WP 6.5.2

function get_taxonomy_labels( $tax ) {
	$tax->labels = (array) $tax->labels;

	if ( isset( $tax->helps ) && empty( $tax->labels['separate_items_with_commas'] ) ) {
		$tax->labels['separate_items_with_commas'] = $tax->helps;
	}

	if ( isset( $tax->no_tagcloud ) && empty( $tax->labels['not_found'] ) ) {
		$tax->labels['not_found'] = $tax->no_tagcloud;
	}

	$nohier_vs_hier_defaults = WP_Taxonomy::get_default_labels();

	$nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name'];

	$labels = _get_custom_object_labels( $tax, $nohier_vs_hier_defaults );

	$taxonomy = $tax->name;

	$default_labels = clone $labels;

	/**
	 * Filters the labels of a specific taxonomy.
	 *
	 * The dynamic portion of the hook name, `$taxonomy`, refers to the taxonomy slug.
	 *
	 * Possible hook names include:
	 *
	 *  - `taxonomy_labels_category`
	 *  - `taxonomy_labels_post_tag`
	 *
	 * @since 4.4.0
	 *
	 * @see get_taxonomy_labels() for the full list of taxonomy labels.
	 *
	 * @param object $labels Object with labels for the taxonomy as member variables.
	 */
	$labels = apply_filters( "taxonomy_labels_{$taxonomy}", $labels );

	// Ensure that the filtered labels contain all required default values.
	$labels = (object) array_merge( (array) $default_labels, (array) $labels );

	return $labels;
}