<?php $the_cat = get_the_category(); $category_name = $the_cat[0]->cat_name; $category_link
= get_category_link( $the_cat[0]->cat_ID ); ?>

The code above is cousing alerts:

Notice: Undefined offset: and Notice: Looking to get property of non-object in

How you can solve the issue to be able to accomplish exactly the same result?

On index.php and archive.php the ame sample of code works fine.

Problem solved:

Solution:

<?php
$the_cat = get_the_category();
$count=0;
if ($the_cat) {
    foreach($the_cat as $cat) {
        $count++;
        echo '<a href="'.get_category_link($cat->cat_ID).'">'.$cat->cat_name.'</a>';
        if( $count == 1 ) break;
    }
}
?>