Last active
May 27, 2016 22:07
-
-
Save mikejolley/485d52994a7643f799fa to your computer and use it in GitHub Desktop.
Add product tag and category classes to loops + post classes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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