Get First Category of Post

Sometimes we need to get or print only first category of the post while developing a WordPress theme. Following template tag will be helpful for displaying first category of post in your theme.


 * Get first category of post.
 * Prints first category for the current post.
 * @return void

function wpflask_first_category_of_post() {

	// Categories of post.
	$category = get_the_category();
	if( $category[0] ) :

	<span class="first-category">
		<a href="<?php echo esc_url( get_category_link( $category[0]->term_id ) ); ?>">
			<?php echo esc_html( $category[0]->cat_name ); ?>