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 looking for a way to display a plain-text list of tags to use as classes on my post elements, I've been trying

$tags = get_tags();
$tag_list = "";
foreach($tags as $tag){
    $tag_list .= $tag->name . " ";
}
echo "<li class=\"$tag_list\">";

in the loop but it seems to output all the tags instead of just the current post's tags, so if I have tags x, y, and z, and I'm viewing a post with tag x I still get <li class="x y z"> anybody have any ideas as to how to show a plain-text list of tags or what I'm doing wrong?

share|improve this question

3 Answers 3

up vote 3 down vote accepted

You can play with arguments to only fetch what you need and get rid of loop:

$classes = implode(' ', wp_get_post_tags( get_the_ID(), array('fields' => 'names') ) );
share|improve this answer
    
Gotta love one-liners... I've supplied an answer below for doing this with custom taxonomies... –  JKirchartz Jun 2 '11 at 17:48

Rarst got me off to a good start, but if you want to do this with a custom taxonomy you should use

$classes = implode(' ', wp_get_object_terms($post->ID, 'custom_post_type', array('fields'=>'names')) );

share|improve this answer

Use wp_get_post_tags instead:

$tags = wp_get_post_tags($post->ID);
$tag_list = "";
foreach($tags as $tag){
    $tag_list .= $tag->name . " ";
}
echo "<li class=\"$tag_list\">";
share|improve this answer
1  
This works, but I gotta give props to the one-liner... –  JKirchartz Jun 2 '11 at 13:00

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.