Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Mamaduka
Last active October 12, 2015 02:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mamaduka/3958721 to your computer and use it in GitHub Desktop.
Save Mamaduka/3958721 to your computer and use it in GitHub Desktop.
Add parent category class to post_class() function
<?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