Apparently, wordpress has a ready script for this. it goes like this:
<?php if ( in_category( $id ) || post_is_in_descendant_category( $id ) ) {
// your code goes here
}
?>
but you need to add a function to your theme's function.php file. this is the function to add:
<?php
if ( ! function_exists( 'post_is_in_descendant_category' ) ) {
function post_is_in_descendant_category( $cats, $_post = null ) {
foreach ( (array) $cats as $cat ) {
// get_term_children() accepts integer ID only
$descendants = get_term_children( (int) $cat, 'category' );
if ( $descendants && in_category( $descendants, $_post ) )
return true;
}
return false;
}
}
?>