I just search for hours to find a solution for my problem. I need to add a body class to my category page and want to apply this class also on all subcategories and posts of the parent category.
For example i want category --> subcategory --> post to have the same class "orange" in body tag of HTML so i can style it properly.
Thanks for any advice.
Sounds like you're putting in too much effort to get a class 'orange' when you can use one of the classes automatically provided to you.
A typical wordpress body class looks something like this
single single-listing postid-3259 custom-header header-image content-sidebar agentpress-pro-blue windows chrome override
There may be more or less but there is usually a class you can use to target a specific page ('postid-3259') or a group of pages ('single-listing').
Without seeing the page(s) you want to target I can't get more specific. Once you have your body class selected you can select any element on the page by adding more selectors.
If you're dead-set on adding an orange class this link may help.
https://css-tricks.com/snippets/wordpress/add-category-name-body_class/
You could utilize the body_class filter and some conditional tags to do this.
add_filter( 'body_class', 'add_parent_category_to_body_class' );
function add_parent_category_to_body_class( $classes ) {
if ( !is_category() && !is_single() ) {
return $classes;
}
$categories = get_the_category();
foreach ($categories as $category)
if ($category->category_parent == 0 ) {
$classes[] = $category->slug;
} else {
$parent = get_category($category->category_parent);
$classes[] = $parent->slug;
}
return $classes;
}