Use category slug as CSS class


/ Published in: PHP
Save to your folder(s)

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!


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.