4

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 );

3

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;

}
5
  • thanks for replying. I'm not lookign for get_terms, but for the taxonomie's name. I'll edit my initial question to be more specific
    – Lore
    Sep 13 '16 at 13:31
  • Oh, ok you meant for categories you wanted to retrieve Categoy and not the actual terms in that taxonomie. My bad, I got that wrong :P
    – bynicolas
    Sep 13 '16 at 13:43
  • getting things mixed up, sorry... Malisa's solution works
    – Lore
    Sep 13 '16 at 14:30
  • Well you need to set the second parameter of get_object_taxonomies to specify you want an object, you hadn't done that so this might be the reason it didn't work. My solution prints the singular name of the tax, are you saying it's not working for you?
    – bynicolas
    Sep 13 '16 at 14:36
  • 2
    sorry I'm not careful enough... I forgot , 'object' in get_object_taxonomies. So both yours and Malisa's solutions work. Thanks a million
    – Lore
    Sep 13 '16 at 14:49
0

Now, I'm not really up to speed on object, object terms, so please be kind.. @This will return what your after, but not 100% sure its the right way, open to improvement from professional :)

$taxonomies = get_object_taxonomies('dt_properties');

foreach($taxonomies as $tax) {
       $each_tax = get_taxonomy($tax);
       echo '<pre>';
       print_r($each_tax->labels->singular_name);
       echo '</pre>';
}
4
  • Very Welcome, I learned something new myself by trying to solve this and with @bynicolas confirming similar way, it seems the right way :)
    – Malisa
    Sep 13 '16 at 14:40
  • Yes it works, but my guess is that your solution might not be as optimal under heavier load. You see, get_object_taxonomies already is filtering the global $wp_taxonomies. So if you return the object instead of just the names, you will have the label to work with. What you are doing is filtering the name attached to a post type and then refiltering all the taxonomies to get the labels for those names. So yes it works, but my solution will do less operations to achieve the same thing and is shorter to write :) @Lore
    – bynicolas
    Sep 13 '16 at 14:50
  • @bynicolas Understood now, I didnt know about setting the second parameter to return the whole object.. when I was trying,I print_r the $tax and didnt get an array, so assumed I needed to then use the tax type to get the taxonomy as an object.. if that makes sense.. it does to me now.. Thanks
    – Malisa
    Sep 13 '16 at 15:03
  • Glad I could clarify this for you. I'm sure your solution won't provide that much more overhead, but surely on a busy site it could make a noticeable improvement to minimize the number of operations.
    – bynicolas
    Sep 13 '16 at 15:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.