wordpress:獲取某篇文章的所有分類,並轉成逗號間隔的字串!

在single.php(單篇文章中)加入以下程式碼:

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

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


參考文章:https://developer.wordpress.org/reference/functions/get_the_category/