Single Post/CPT Category/Taxonomy Template Tags

Below are functions to display categories or taxonomies attached to a single post or custom post type. Some functions work only on the built in Post ‘category’. Others work on taxonomies which can be attached to the built in Post or a Custom Post Type.

Display Single Post’s Categories

<?php $output = get_the_category_list(string $separator = '', string $parents = '', int $post_id = false); ?>

or

<?php the_category( string $separator = '', string $parents = '', int $post_id = false ); ?> //outputs

Display Custom Taxonomies attached to Post or Custom Post Type

get_the_terms( int|WP_Post $post, string $taxonomy );
//returns Array of WP_Term objects attached to post/CPT

get_the_term_list(int $id, string $taxonomy, string $before = '', string $sep = '', string $after = '');
//Returns string of formatted taxonomies attached to post/CPT.

wp_get_post_terms( int $post_id, string|array $taxonomy = 'post_tag', array $args = array() );
//Returns array of WP_Terms objects attached to post/CPT just like get_the_terms, but you can limit fields by passing $args which might be useful if want to do an array merge on the results. 

Get Post’s Categories/Taxonomies as an array of objects

$cats = get_the_terms( int|WP_Post $post, string $taxonomy );

//or get_the_category( int $id = false ) gets only categories and calls 
get_the_terms( $id, 'category' ); in first line of the function.
Do NOT follow this link or you will be banned from the site!