Get Taxonomy Labels
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 likeCategory,Tag, orProduct category.plural: returns labels likeCategories,Tags, orProduct 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
- Procedural
- OOP
<?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' );
}
}
<?php
/**
* Taxonomy label helpers.
*
* @package mac-core
*/
declare(strict_types=1);
namespace MacCore\Utils {
use WP_Taxonomy;
use WP_Term;
/**
* Taxonomy label helpers.
*/
final class GetTaxonomyLabels
{
/**
* Get a singular or plural label for a taxonomy.
*
* @param int|string|WP_Term|null $termOrTax Taxonomy slug, term ID, term object, or null for category.
*/
public static function get(
int|string|WP_Term|null $termOrTax = 'category',
string $type = 'singular',
string $fallback = ''
): string {
$type = \strtolower( \trim( $type ) );
$type = $type === 'plural' ? 'plural' : 'singular';
$taxonomy = self::resolveTaxonomySlug( $termOrTax );
if ( $taxonomy === null ) {
return $fallback !== '' ? $fallback : ( $type === 'plural' ? 'Terms' : 'Term' );
}
$object = \get_taxonomy( $taxonomy );
if ( ! $object instanceof WP_Taxonomy ) {
return $fallback !== '' ? $fallback : ( $type === 'plural' ? 'Terms' : 'Term' );
}
if ( $type === 'plural' ) {
$label = (string) ( $object->labels->name ?? '' );
return $label !== '' ? $label : ( $fallback !== '' ? $fallback : 'Terms' );
}
$label = (string) ( $object->labels->singular_name ?? '' );
return $label !== '' ? $label : ( $fallback !== '' ? $fallback : 'Term' );
}
/**
* @param int|string|WP_Term|null $termOrTax
*/
public static function singular( int|string|WP_Term|null $termOrTax = 'category', string $fallback = '' ): string
{
return self::get( $termOrTax, 'singular', $fallback );
}
/**
* @param int|string|WP_Term|null $termOrTax
*/
public static function plural( int|string|WP_Term|null $termOrTax = 'category', string $fallback = '' ): string
{
return self::get( $termOrTax, 'plural', $fallback );
}
/**
* @param int|string|WP_Term|null $termOrTax
*/
private static function resolveTaxonomySlug( int|string|WP_Term|null $termOrTax = 'category' ): ?string
{
if ( $termOrTax instanceof WP_Term ) {
$taxonomy = \sanitize_key( (string) $termOrTax->taxonomy );
return $taxonomy !== '' && \taxonomy_exists( $taxonomy ) ? $taxonomy : null;
}
if ( \is_int( $termOrTax ) || ( \is_string( $termOrTax ) && \is_numeric( $termOrTax ) ) ) {
$term = \get_term( (int) $termOrTax );
if ( $term instanceof WP_Term ) {
$taxonomy = \sanitize_key( (string) $term->taxonomy );
return $taxonomy !== '' && \taxonomy_exists( $taxonomy ) ? $taxonomy : null;
}
return null;
}
if ( $termOrTax === null || $termOrTax === '' ) {
$termOrTax = 'category';
}
$taxonomy = \sanitize_key( (string) $termOrTax );
return $taxonomy !== '' && \taxonomy_exists( $taxonomy ) ? $taxonomy : null;
}
}
}
namespace {
if ( ! function_exists( 'mac_get_taxonomy_label' ) ) {
function mac_get_taxonomy_label( string $taxonomy = 'category', string $type = 'singular', string $fallback = '' ): string
{
return \MacCore\Utils\GetTaxonomyLabels::get( $taxonomy, $type, $fallback );
}
}
if ( ! function_exists( 'mac_get_taxonomy_singular' ) ) {
function mac_get_taxonomy_singular( int|string|\WP_Term|null $term_or_tax = 'category', string $fallback = '' ): string
{
return \MacCore\Utils\GetTaxonomyLabels::singular( $term_or_tax, $fallback );
}
}
if ( ! function_exists( 'mac_get_taxonomy_plural' ) ) {
function mac_get_taxonomy_plural( int|string|\WP_Term|null $term_or_tax = 'category', string $fallback = '' ): string
{
return \MacCore\Utils\GetTaxonomyLabels::plural( $term_or_tax, $fallback );
}
}
}