在single.php(單篇文章中)加入以下程式碼:
1 2 3 4 5 6 7 8 9 10 |
$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/