I am using this code to retrieve the category of the current post.
<?php $category = get_the_category();
$firstCategory = $category[0]->cat_name; echo $firstCategory;?>
How could I get the category URL without running another database query?
Pass the category id into get_category_link()
:
<?php
$category = get_the_category();
$link = get_category_link( $category[0]->term_id );
?>
Update Outputting in template:
<?php
$category = get_the_category();
$first_category = $category[0];
echo sprintf( '<a href="%s">%s</a>', get_category_link( $first_category ), $first_category->name );
?>