Last active
October 12, 2015 02:38
-
-
Save Mamaduka/3958721 to your computer and use it in GitHub Desktop.
Add parent category class to post_class() function
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
<?php | |
/** | |
* Add parent category class to post_class() function | |
* | |
* @link http://wordpress.stackexchange.com/q/23259#23260 | |
*/ | |
function mamaduka_add_parent_category_class( $classes, $class, $post_id ) { | |
foreach ( (array) get_the_category( $post_id ) as $cat ) { | |
if ( ! empty( $cat->parent ) ) { | |
$parent_cat = &get_category( $cat->parent ); | |
$classes[] = 'category-parent-' . sanitize_html_class( $parent_cat->slug, $parent_cat->term_id ); | |
} else { | |
$classes[] = 'category-parent-' . sanitize_html_class( $cat->slug, $cat->term_id ); | |
} | |
} | |
return $classes; | |
} | |
add_filter( 'post_class', 'mamaduka_add_parent_category_class', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment