I have a custom post type called "Projects" and for it some categories.
I would like to display the category name of the posts in the loop but only once.
I am using this but the category is being displayed on top of every post.
For example if the 2 posts are in the same cetegory, now I see the category name twice in the same loop.
How can I solve it?
My code:
<?php
$category = the_category();
?>
<h2 style="text-transform: uppercase;">
<?php
if($i == 1) {
echo $category[1]->cat_name;
}
$i++;
?>
</h2>
Many thanks in advance
$i
to1
before The Loop and changethe_category()
toget_the_category()
(the_category()
prints category names -get_the_category()
retrieves an array of category objects as your code expects). Also note that in PHP array indexes start at0
- the first category object would be in$category[0]
, not$category[1]
.