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 create <div class="cat-hidden categories"></div> where categories is a list of the categories the current post is posted in.

When just using the_taxonomies(), it outputs something like this:

<div class="cat-hidden Job Type: &lt;a href='http://www.cirkut.net/wp/libertyguide/genre/early-career/'&gt;Early-Career&lt;/a&gt;, &lt;a href='http://www.cirkut.net/wp/libertyguide/genre/internship/'&gt;Internship&lt;/a&gt;, &lt;a href='http://www.cirkut.net/wp/libertyguide/genre/other/'&gt;Other&lt;/a&gt;, and &lt;a href='http://www.cirkut.net/wp/libertyguide/genre/web-developmentit/'&gt;Web Development/IT&lt;/a&gt;."></div>`

Whereas I want it to output something similar to this:

<div class="cat-hidden Early-Career Internship Web-Development/IT"></div>

That means I'm going to also have to parse the categories that are outputted because I need Web Development/IT to be Web-Development/IT or Web-Development-IT. I'm unsure about the / being in there because I think CSS doesn't allow a /. That's where you experts come along.

I am very proficient in HTML and CSS, and know some PHP and Wordpress, but I don't know where to begin.

If any more info is needed, please let me know.

Thank you!

share|improve this question

2 Answers 2

up vote 3 down vote accepted

you can use wp_get_post_terms() to get a list of the post categories and just output the category slug which is already phrased for you, something like this:

//in your loop
echo '<div class="cat-hidden';
$cats = wp_get_post_terms($post->ID,'category');
foreach($cats as $cat){
   echo ' '.$cat->slug;
}
echo '"></div>';
share|improve this answer
    
I checked your solution and it works!! Luckily I had found another way too, by doing <div class="hidden tags <?php $terms = wp_get_object_terms($post->ID,'jobtype'); foreach($terms as $term){echo $term->slug . ' ';} ?>"></div> –  Josh Allen Jul 29 '11 at 23:30
    
I'm unsure what the difference between wp_get_object_terms() and wp_get_post_terms() is, but they both seem to achieve the same thing. Anyways, thank you for a perfect and quick solution :) –  Josh Allen Jul 29 '11 at 23:34
2  
wp_get_post_terms is just a wrapper function for wp_get_object_terms, they do both actually do the same thing. –  t31os Jul 30 '11 at 9:53

Just to offer up some different approaches ...

$terms = get_the_terms( $post->ID, 'category' );
$terms = ( $terms ) ? wp_list_pluck( $terms, 'slug' ) : array();
$space = !empty( $terms ) ? ' ' : '';
printf( '<div class="cat-hidden%s"></div>', $space . implode( ' ', $terms ) );

Another ...

$terms = get_the_terms( $post->ID, 'category' );
$terms = ( $terms ) ? wp_list_pluck( $terms, 'slug' ) : array();
$classes = !empty( $terms ) ? ' ' . implode( ' ', $terms ) : '';
echo "<div class='cat-hidden$classes'></div>";

That wp_list_pluck function is real handy for these kind of things, avoids the need to do any looping yourself..

share|improve this answer
    
Thanks for the alternatives. I'll save these and use these in the future. –  Josh Allen Jul 31 '11 at 20:01
    
You're welcome... :) –  t31os Aug 19 '11 at 13:08

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.