Created
October 10, 2011 10:51
-
-
Save Mamaduka/1275029 to your computer and use it in GitHub Desktop.
Add parent category class to post_class() function
This file contains 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_filter( 'post_class', 'mamaduka_add_parent_category_class' ); | |
/** | |
* Add parent category class to post_class() function | |
* | |
* @link http://wordpress.stackexchange.com/q/23259#23260 | |
*/ | |
function mamaduka_add_parent_category_class( $classes ) { | |
global $post; | |
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; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment