0

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..

3 Answers 3

1

@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).

2
  • 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) Commented Jan 19, 2011 at 22:16
  • Thanks for your help I managed to add the taxonomy term_id to the post_class function
    – tbwcf
    Commented Jan 20, 2011 at 15:54
1

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; ?>">
2
  • 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
    Commented Jan 20, 2011 at 15:53
  • yes, you would need to change echo $term->slug; with echo $term->slug." ";. I forgot to do that... Commented Jan 20, 2011 at 19:23
0

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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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