Scroll to top

Get post category names with separator in loop

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:
Author avatar
Zahid Iqbal
I am a web designer and developer working full-time.

Post a comment

Your email address will not be published.

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