To display a list of categories associated with a post, separated by commas

Mayank Gupta WordPress 0 Comments

To display a list of categories associated with a post, separated by commas, write this code:

<?php
$cats = array();
foreach (get_the_category($post_id) as $c) {
$cat = get_category($c);
array_push($cats, $cat->name);
}

if (sizeOf($cats) > 0) {
$post_categories = implode(', ', $cats);
} else {
$post_categories = 'Not Assigned';
}

echo $post_categories;
?>

Leave a Reply

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