Skip to main content

Get Taxonomy Labels

PropertyValue
descriptionGet singular or plural labels for a taxonomy.
tagslib, php, wp, oop
rating

Overview

Get the singular or plural label for a taxonomy. The procedural helper accepts a taxonomy slug and defaults to category.

Supported label types:

  • singular: returns labels like Category, Tag, or Product category.
  • plural: returns labels like Categories, Tags, or Product categories.

Pass a fallback string when you want a custom value for missing or invalid taxonomies. The OOP variant also accepts a term ID or WP_Term object for the singular/plural convenience wrappers.

Variants

<?php
/**
* Get taxonomy label.
*
* Args:
* taxonomy -> default = category
* type -> singular | plural (default = singular)
* fallback -> optional override
*
* Examples:
* mac_get_taxonomy_label();
* mac_get_taxonomy_label( 'post_tag', 'plural' );
*
* @param string $taxonomy Taxonomy slug.
* @param string $type Label type: singular or plural.
* @param string $fallback Optional fallback label.
* @return string
*/

if ( ! function_exists( 'mac_get_taxonomy_label' ) ) {

function mac_get_taxonomy_label( string $taxonomy = 'category', string $type = 'singular', string $fallback = '' ): string {

$taxonomy = sanitize_key( $taxonomy );
$type = strtolower( trim( $type ) );
$type = $type === 'plural' ? 'plural' : 'singular';

if ( $taxonomy === '' || ! taxonomy_exists( $taxonomy ) ) {
return $fallback ?: ( 'plural' === $type ? 'Terms' : 'Term' );
}

$tx = get_taxonomy( $taxonomy );

if ( ! $tx || empty( $tx->labels ) ) {
return $fallback ?: ( 'plural' === $type ? 'Terms' : 'Term' );
}

if ( 'plural' === $type ) {
return $tx->labels->name ?: ( $fallback ?: 'Terms' );
}

return $tx->labels->singular_name ?: ( $fallback ?: 'Term' );
}
}