WordPress

While creating custom post type detail template, there may be need to display list of categories or taxonomies related to it.

For example

wp_get_object_terms()

To display list of all the terms use wp_get_object_terms

$tax_lists = wp_get_object_terms($post->ID, 'articles_cat'); 
// here articles_cat is taxonomy slug

To display list of all the terms on post detail page by name

 $tax_lists = wp_get_object_terms($post->ID, 'articles_cat',array("fields" => "name")); 
// here articles_cat is taxonomy slug 

To display category on post detail page

using wp_get_object_terms() categories of a post can be displayed on post detail page i.e. single.php

$cat_lists = wp_get_object_terms($post->ID, 'category'); 
// here articles_cat is taxonomy slug 

What is alternative of get_the_category() on post detail page

use wp_get_object_terms() instead of get_the_category() to get list of terms related to a post

Get values of all terms of a custom taxonomy

 $terms = get_terms([     'taxonomy' => $taxonomy,     'hide_empty' => false, ]); 

Leave a comment