Skip to content

Instantly share code, notes, and snippets.

@mikejolley
Last active May 27, 2016 22:07
Show Gist options
  • Save mikejolley/485d52994a7643f799fa to your computer and use it in GitHub Desktop.
Save mikejolley/485d52994a7643f799fa to your computer and use it in GitHub Desktop.
Add product tag and category classes to loops + post classes
/**
* Code placed in theme functions.php
*/
add_filter( 'post_class', 'wc_product_tag_post_class', 20, 3 );
function wc_product_tag_post_class( $classes, $class = '', $post_id = '' ) {
$tags = get_the_terms( $post_id, 'product_tag' );
if ( ! empty( $tags ) ) {
foreach ( $tags as $key => $value ) {
$classes[] = 'product-tag-' . $value->slug;
}
}
$categories = get_the_terms( $post_id, 'product_cat' );
if ( ! empty( $categories ) ) {
$slugs = wp_list_pluck( $categories, 'slug' );
foreach ( $slugs as $slug ) {
$classes[] = 'product-cat-' . $slug;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment