[WordPress] Output category without link
To output the category associated with a WordPress post, you usually use <? Php the_category ();?>, But in that case you will also get a link.
I wanted to get rid of this link. The implementation method was to store it in an array once using Php get_the_category ();?> And display it in a loop. Also, when displaying multiple categories, a separate (,) is added to make it easier to see, but conditional branching is also made so that the separator is not attached when the last category is output.
<? php
$ categories = get_the_category ();
$ length = count ($ categories);
$ i = 0;
foreach ($ categories as $ cat) {
$ i ++;
if ($ length == $ i) {
echo $ cat-> cat_name;
} else {
echo $ cat-> cat_name.',';
}
}
?>
use implode
When I posted the above, I received a comment that it can be implemented simply by using implode as shown below.
Thank you, technote-space!
<? php
$ categories = get_the_category ();
echo implode (',', array_map (function ($ cat) {return $ cat-> cat_name;}, $ categories));
?>