How to get the singular name of a custom taxonomy?

I have got something like this:

$taxonomies = get_object_taxonomies('my_post_type');

foreach($taxonomies as $tax){

  $args['labels'] = get_taxonomy_labels( (object) $args );
  $args['label'] = $args['labels']->singular_name;
  echo $args['label'];

}

which gives me “Tag” each time, for post_tags but also for all the other (custom) taxonomies.
How can I get the label for each $tax ?

Thanks

ps : by label I mean labels I register when creating my custom taxonomy like this $labels = array(
'name' => my_custom_taxonomies,
'singular_name' => my_custom_taxonomy
);

2 Answers
2

EDIT
Since I missunderstood your question at first, here’s the update that should do what you want.

$taxonomies = get_object_taxonomies( 'my_post_type', 'object' );

foreach($taxonomies as $tax){

  echo $tax->labels->singular_name;

}

Basically, you need to specify to the get_object_taxonomies function that you want an object to be returned.

In your function, I’m not sure where the $args come from and put as is, it can’t work.

Finally, use the correct syntax to work with an object. You access the object property with ->


ORIGINAL

I believe you are looking for the get_terms() function. So you would have something like this:

// Retrieve the taxonomies for your custom post type
$cpt_taxes = get_object_taxonomies( 'my_post_type', 'object' );

// Build an array of taxonomies slugs to be used in our $args array below to filter only taxes we need.
foreach( $cpt_taxes as $cpt_tax ){
  $taxonomies[] = $cpt_tax->name;
}

// Supply our $args array for the get_terms() function with our newly created $taxonomies array.
$args = array( 
  'taxonomy' => $taxonomies,
  'hide_empty' => false,      
);
$terms = get_terms( $args );


// Go over the results of the get_terms function and echo each term name.
foreach( $terms as $term ){

  echo $term->name;

}

Leave a Comment