If you are developer of subjects or plugins WordPress is possible that sometimes you have needed to know if a post belongs to one of the categories root. Some functions in WordPress exist that serve to cross ascending and descendent the tree of categories but they give back the result in the form of representation HTML and we do not want it thus, we directly want the result in object format or the slug of the category.

Here we put a function that solves the problem easily, you only must change line 7 where it puts €˜categoria_raiz€™ by the slug of the category root that you are looking for:

1 2 3 4 5 6 7 8
function pertenece_a_categoria_raiz ($post_id) \ {$cat = get_the_category ($post_id);
	$cat = $cat [0];
	while ($cat->parent! = 0)
		$cat = get_category ($cat->parent);
	return $cat->slug == €˜categoria_raiz€™;}