Function and usage of get_taxonomy_labels() in wordpress

Question

Answers ( 1 )

    0
    2024-01-07T21:21:01+00:00

    The get_taxonomy_labels() function in WordPress is used to build an object containing all the labels for a given taxonomy, such as categories or tags. This function is particularly useful when you need to customize or retrieve the labels associated with taxonomies in WordPress. Here's a detailed explanation and a sample usage:

    Function Overview

    • Function Signature: get_taxonomy_labels( WP_Taxonomy $tax ): object
    • Purpose: To build an object containing all the labels for a given taxonomy.
    • Parameters:
      • $tax (WP_Taxonomy): This is the taxonomy object for which labels are to be built.
    • Return: An object containing taxonomy labels.

    Label Object Details

    The function returns an object with the following properties, each representing a label:

    • name: General name for the taxonomy, usually plural. Defaults to 'Tags' for non-hierarchical and 'Categories' for hierarchical taxonomies.
    • singular_name: Name for one object of this taxonomy. Defaults to 'Tag' or 'Category'.
    • search_items: Label for search functionality. Defaults to ‘Search Tags’ or 'Search Categories'.
    • popular_items: Label for popular items, used only for non-hierarchical taxonomies. Default is ‘Popular Tags’.
    • all_items: Label for all items. Defaults to ‘All Tags’ or 'All Categories'.
    • parent_item: Only for hierarchical taxonomies. Default is ‘Parent Category’.
    • parent_item_colon: Same as parent_item but with a colon at the end.
    • name_field_description: Description for the Name field on Edit Tags screen.
    • slug_field_description: Description for the Slug field.
    • parent_field_description: Description for the Parent field on Edit Tags screen, used for hierarchical taxonomies.
    • desc_field_description: Description for the Description field on Edit Tags screen.
    • edit_item: Label for editing an item. Defaults to ‘Edit Tag’ or 'Edit Category'.
    • view_item: Label for viewing an item. Defaults to ‘View Tag’ or 'View Category'.
    • update_item: Label for updating an item. Defaults to ‘Update Tag’ or 'Update Category'.
    • add_new_item: Label for adding a new item. Defaults to ‘Add New Tag’ or 'Add New Category'.
    • new_item_name: Label for naming a new item. Defaults to ‘New Tag Name’ or 'New Category Name'.
    • separate_items_with_commas: Used only for non-hierarchical taxonomies. For meta box.
    • add_or_remove_items: Used only for non-hierarchical taxonomies when JavaScript is disabled.
    • choose_from_most_used: Used in the meta box for non-hierarchical taxonomies.
    • not_found: Used in the meta box and taxonomy list table. Defaults to ‘No tags found’ or 'No categories found'.
    • no_terms: Used in posts and media list tables. Defaults to ‘No tags’ or 'No categories'.
    • filter_by_item: Only for hierarchical taxonomies. Used in posts list table.
    • items_list_navigation: Label for table pagination hidden heading.
    • items_list: Label for the table hidden heading.
    • most_used: Title for the Most Used tab.
    • back_to_items: Label displayed after a term has been updated.
    • item_link: Used in the block editor for navigation link block variation.
    • item_link_description: Description for a navigation link block variation in the block editor.

    Sample Usage

    // Assuming you have a taxonomy object $taxonomy
    $labels = get_taxonomy_labels( $taxonomy );
    
    // Example of accessing a specific label
    echo $labels->add_new_item; // Outputs ‘Add New Tag’ or 'Add New Category' depending on the taxonomy
    

    This function is useful for theme and plugin developers who need to work with custom labels for taxonomies, providing a standardized way to access and utilize these labels in WordPress.

Leave an answer