I was working on a theme recently and have to show category names (comma separated). So, i found this useful piece of code. You can change the comma inside quotes (‘, ‘) in last line to anything you like for separator.

$categories = get_the_category();
$category_names = array();
foreach ($categories as $category)
{
    $category_names[] = $category->cat_name;
}
echo implode(', ', $category_names);
Share Post:

Posted by Zahid Iqbal

I am a web designer and developer working full-time.

Leave a reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.