Get the first parent category only from a WordPress post

Add this to your theme functions.php

// get the first parent category only
function get_the_first_parent_category_only($id)
{
	$categories = get_the_category($id);
	$parent_categories = array();
	if($categories){
		foreach($categories as $category) {
			if ($category->parent == 0)
			{
				$parent_categories[] = $category;
			}
		}
	}
	return (isset($parent_categories[0])) ? $parent_categories[0] : false;
}

Then display a category with link like this (in index.php or single.php):

<!-- Print a link to this category -->
$category = get_the_first_parent_category_only(get_the_ID()); 
<a href="<?php echo esc_url( get_category_link( $category_id ) ); ?>" title="<?php echo $category->name; ?>"><?php echo $category->name; ?></a>