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