I created a custom function in functions.php to check if post_is_in_descendant_category and I'm controlling it from a plugin which is returning true is a post is in descendant category, it is working well
But it's not working in my home.php, the strange thing is that when I check if post_is_in_descendant_category for any post in home.php result stuck to true
I want to know the reason for that and how to fix it
my code for post_is_in_descendant_category
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;
}
}
my plugin code:
function check_category(){
global $rtlDir;
if ( $category_to_check = get_term_by( 'slug', 'category-3', 'category' )){
if(post_is_in_descendant_category($category_to_check->term_id)){
$rtlDir = true;
}else{
$rtlDir = false;
}
}
}
add_filter("wp","check_category");