How to get all categories associated with the current post in custom post type template in WordPress?

 

Get all categories associated with the current post in custom post type template in WordPress




With this code, you will be easily able to get all the categories of the current associated post. Just copy and paste this into your custom post-type template. Enjoy.


<?php 
// Get all the cats of current post 
// Here I am taking taonomy name = case_category.
$category = get_the_terms( $post->ID, 'case_category' );
if($category){
echo "<h3>Category:</h3>";
$all_cat_lists = "";
foreach ( $category as $cat){
$all_cat_lists .= "<a href=".get_category_link( $cat->term_id )."><span>".$cat->name."</span></a>, "; 
        echo rtrim($all_cat_lists, ", ");
}

?> 


Get all categories associated with the current post in custom post type template in WordPress.