I want to display only 3 categories (Ex: horses, dogs, birds), names only and comma separated, in my Post, since one of them, two or all tree are marked in the post.
<span><?php
if ( 'in_category('horses') ) {
echo "horses";
} ?></span><span><?php
if ( in_category('dogs') ) {
echo "dogs";
} ?></span><span><?php
if ( in_category('birds') ) {
echo "birds";
}
?></span>
It should be enough using a single <span>
for all categories and add some logic.:
<span><?php
$categories = ['horses','dogs','birds'];
$string = "";
foreach ($categories as $category){ //iterate over the categories to check
if(has_category($category))
$string .= $category.", "; //if in_category add to the output
}
$string = trim($string); //remove extra space from end
$string = rtrim($string, ','); //remove extra comma from end
echo $string; //result example: <span>horses, dogs</span>
?></span>