One way of doing it would be to modify your current code and add the links in there:
function category_name_shortcode() {
global $post;
$post_id = $post->ID;
$catName = "";
foreach((get_the_category($post_id)) as $category){
$catName .= '<a href="' . get_term_link($category) . '">' . $category->name . '</a>, ';
}
return $catName;
}
add_shortcode( 'post_category', 'category_name_shortcode' );
But there’s an easier way, because WP already has a way to obtain the list of categories with links (get_the_category_list
):
function category_name_shortcode() {
return get_the_category_list( ', ' );
}
add_shortcode( 'post_category', 'category_name_shortcode' );