Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to output the taxonomies associated with a post in a 'title' attribute, so it needs to be unformatted. I know about get_terms and get_terms_list, but the problem is that you need to provide which taxonomy you want to get on forehand. But what if you have an archive page which lists multiple different post_types... you don't know which taxonomies are associated with a certain post as you don't know which post-type it is. So I tried this:

$posttaxonomies = get_the_taxonomies();
  if ($posttaxonomies) {
    foreach($posttaxonomies as $taxonomy) {
      $thetaxonomies .= $taxonomy->name . ' '; 
  }  
}

which doesn't output anything. I know I must be doing something wrong, but am stumped with it.

share|improve this question
add comment

1 Answer 1

up vote 3 down vote accepted

if you are in the loop you can use get_post_taxonomies like so:

$taxs = get_post_taxonomies($post->ID);
foreach ($taxs as $tax){
    // $tax holds the taxonomy name so you can
    //use either get_terms or get_terms_list
}

Update

I'll try to explain better, since you don't know what the post type is and you can't tell what taxonomies are associated with that type you first get the list of taxonomies that are associated with each post (no matter what the post type is) like so:

$taxs = get_post_taxonomies($post->ID);

now $taxs is an array that holds the names of the taxonomies associated with the current post in the loop. so we can run a foreach loop to output the post specific terms for each taxonomy using get_terms or get_terms_list for example:

 $taxs = get_post_taxonomies($post->ID);
 foreach ($taxs as $tax){
    $before = $tax . ": ";
    echo get_the_term_list( $post->ID, $tax, $before, ' ', '' );
 }

update 2

Well if you don't wont it to echo post_tags then just check and skip it and as for getting just the terms and not the formatted html use wp_get_object_terms instead of get_the_term_list so something like:

$taxs = get_post_taxonomies($post->ID);
foreach ($taxs as $tax){
    if (!$tax = "post_tags"){ //exclude tags
        print_r(wp_get_object_terms( $post->ID, $tax)); // its an array of the terms
    }
}
share|improve this answer
    
well, I already tried that, but it doesn't output the actual names of the taxonomy, just the general name of it. Let's say I have a taxonomy called product_type with values of 'cd', 'dvd', 'book' etc.. right now it doesn't output if it's a cd or a book or whatever, it outputs 'post_tag' and 'product_type' –  grrrbytes Feb 23 '11 at 10:43
    
So are you referring to terms then? cd, dvd and book would be terms under the taxonomy, not taxonomies themselves(product type is the taxonomy). May i suggest rewording your question to confirm what it is you're asking. –  t31os Feb 23 '11 at 14:33
    
check the answer , i updated to explain better –  Bainternet Feb 23 '11 at 22:37
    
Tnx! This is certainly more in the right direction. Still: it outputs formatted html. Is there any way to output plaintxt without using strip_tags? –  grrrbytes Mar 1 '11 at 21:20
    
and, it also outputs the tags. I just want to have the custom taxonomies... –  grrrbytes Mar 1 '11 at 21:25
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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