CHRISTMAS EDITION
Due to Christmas time and my brain hitting some serious technical difficulties, I got get_the_category()
and get_the_categories
filter mixed up.
Here is the revised filter function, working:
You can also make use of the get_the_categories
filter
add_filter( 'get_the_categories', function ( $categories )
{
// Only return the first object in the array
return $cat[] = $categories[0];
});
ORIGINAL ANSWER
Because you pass everything directly to your foreach
loop, the easiest ist to break
the loop after the first iteration
<?php
foreach((get_the_category()) as $cat)
{
$catname =$cat->category_nicename;
echo "<a href=\"/category/";
echo $catname;
echo "/\">";
echo "<img src=\"/wp-content/categorias/";
echo $catname;
echo ".png\" alt=\"$catname category image\" /></a>\n";
// Lets break the loop after one iteration
break;
}
?>