Skip to content

Instantly share code, notes, and snippets.

@Mamaduka
Created October 10, 2011 10:51
Show Gist options
  • Save Mamaduka/1275029 to your computer and use it in GitHub Desktop.
Save Mamaduka/1275029 to your computer and use it in GitHub Desktop.
Add parent category class to post_class() function
<?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