Get the topmost category in WordPress

If you’re using WordPress, here’s a quick snippet of code to get the oldest ancestor (topmost parent) of a category or subcategory.

This can just go in functions.php.

The only caveat to the snippet is that it will always return only a single category, so if that’s not a problem, you should be fine.