Skip to content

Instantly share code, notes, and snippets.

@xlawok
Created December 1, 2020 10:20
Show Gist options
  • Save xlawok/5ea1eab8bb389f118dd08f28864ba1be to your computer and use it in GitHub Desktop.
Save xlawok/5ea1eab8bb389f118dd08f28864ba1be to your computer and use it in GitHub Desktop.
Wordpress add class to category wp_list_categories
// add class to wp_list_categories
// if category has a children add cat-parent class
add_filter( 'category_css_class', 'add_class_to_category', 10, 4);
function add_class_to_category($css_classes, $category, $depth, $args) {
$children = get_terms( $category->taxonomy, array(
'parent' => $category->term_id,
'hide_empty' => false
) );
// print_r($children); // uncomment to examine for debugging
if($children) { // get_terms will return false if tax does not exist or term wasn't found.
// term has children
$css_classes[] = 'cat-parent';
}
return $css_classes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment