Posted By

Pr0v4 on 05/14/11


Tagged

wordpress category loop


Versions (?)

Use category slug as CSS class


 / Published in: PHP
 

This snippet will help you to use the mother category, the ones with parent => 0, as your classes ready to be stylezed in your CSS!

  1. $categorie = get_the_terms($post->ID, 'ad_cat');
  2. foreach( $categorie as $categoria ){
  3. if( $categoria->parent != 0 ){
  4. $cat = $categoria;
  5. while( $cat->parent != 0){
  6. $cat = get_term( $cat->parent, 'ad_cat');
  7. }
  8. echo '<a href="'.get_term_link($categoria->slug, 'ad_cat').'" class="'.$cat->slug.'" rel="tag">'.$categoria->name.'</a> ';
  9. } else {
  10. echo '<a href="'.get_term_link($categoria->slug, 'ad_cat').'" class="'.$cat->slug.'" rel="tag">'.$categoria->name.'</a> ';
  11. }
  12. }

Report this snippet  

You need to login to post a comment.