My wordpress site has categories with a simple hierarchy –
I'm using the if statement below to catch if the post is in category blog or any of the child categories - and it works - but it feels stupid not to be able to just check the parent of the current category (plus I might want to add categories later).
if ( is_category('blog') || in_category(array('role-shift', 'urod-the-last-show', 'news')) )
I've searched and tried every suggestion - including cat_is_ancestor_of - but nothing is working.
Please help!
Robert
$categories = get_the_category(); //returns categories
$thiscat = $categories[0];
$parent_id = $thiscat->parent; //the parent id
$parent = get_category($parent_id) //this returns the parent category as an object
//use id or slug of category you are searching for
if( $parent_id == 1 || $thiscat->slug == 'blog' ){
//this is a child of blog or the blog page
}
This should do the trick. This will determine if the current category is a child of the blog page. The first part, get_category, returns the current category.
Then you can get the parent id from the current category and use 'get_the_category_by_ID' to get the parent category object.
Then you can check if you are under the parent category you want.