Skip to content

Instantly share code, notes, and snippets.

@ocean90
Created September 14, 2011 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ocean90/1217688 to your computer and use it in GitHub Desktop.
Save ocean90/1217688 to your computer and use it in GitHub Desktop.
Extend post_class with a class for parent category
<?php
/**
* Erweitert post_class() um eine Klasse für Elternkategorien.
*
* @author Dominik Schilling
* @license GPLv2
* @link http://wpgrafie.de/178/
*
* @version 0.2
*/
function ds_add_parent_category_class( $classes ) {
$cats = get_the_category();
if ( empty( $cats ) )
return $classes;
foreach ( $cats as $cat ) {
$parent_cat = get_category( $cat->category_parent );
if ( ! is_wp_error( $parent_cat ) )
$classes[] = 'parent-category-' . sanitize_html_class( $parent_cat->slug, $parent_cat->term_id );
}
return $classes;
}
add_filter( 'post_class', 'ds_add_parent_category_class' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment