I found this thread very useful. Sivar’s function was missing the last line which seemed to fix the problem I was having.
function get_parent_category() {
foreach ((get_the_category()) as $cat) {
if ($cat->category_parent) return $cat->category_parent;
else return $cat->cat_ID;}
}