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'm trying to mark up my posts using their custom taxonomy slug as the div class so that they can be filtered...

So far what I have displays the taxonomy name but what I need is the slug so I have a nice-space-free-name, I have the below so far outputting the name:

 <div class="box-item cocktails-box 
 <?php $terms_as_text = strip_tags( get_the_term_list( $wp_query->post->ID, 'cocktail_type', '', ' ', '' ) );
 echo $terms_as_text; ?>">

Any help would be much appreciated - I'm getting a bald patch from scratching my head on this..

share|improve this question

3 Answers 3

up vote 1 down vote accepted

@Ambitious Amoeba's answer works. But have you looked into the post_class function? It'd do what you want, and save you a lot of trouble. Just use this as your div opener:

<div <?php post_class('box-item cocktails-box'); ?>>

where you pass the classes you want applied to all posts as a list or array, and let WordPress handle the rest (it'll add classes for all taxonomies that the post is in, and you can filter it to add additional class depending on the view, if you want).

share|improve this answer
    
but then he gets a lot of unrelated classes too, and most likely he'll need to add more specific CSS rules to avoid conflicts (this increases CSS size) –  onetrickpony Jan 19 '11 at 22:16
    
Thanks for your help I managed to add the taxonomy term_id to the post_class function –  tbwcf Jan 20 '11 at 15:54

taxonomy slug, or the taxonomy terms slug? I assume you mean the last:

<div class="box-item cocktails-box 
<?php foreach(get_the_terms($wp_query->post->ID, 'cocktail_type') as $term)
         echo $term->slug; ?>">
share|improve this answer
    
Thanks this was close but pulled in the slugs with no spaces so they wouldn't work as classes, I actually ended up getting the term_id by adding some functions to the post_class –  tbwcf Jan 20 '11 at 15:53
    
yes, you would need to change echo $term->slug; with echo $term->slug." ";. I forgot to do that... –  onetrickpony Jan 20 '11 at 19:23

This isn't really answer, but I adapted One Trick Pony's solution into a list for my own purposes. I thought I'd repost in case it might be of use to anyone.

<ul><?php foreach(get_the_terms($post->ID, 'tool' , '<li>', '</li><li>', '</li>' ) as $term)
     echo '<li>'.$term->slug.'</li>' ?></ul>
share|improve this answer

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.