How to Display Custom Post Type Category (With Name & Link!)

In this article, I will take a look at getting and displaying a custom post type’s categories, category name, link, and list of all the categories of the custom post types.

But before moving further, let say I have a custom post type called Events which has slug ‘events’ and it has a taxonomy called Categories which has slug ‘events-cat’ as shown in the image below.

We will use this case, for all the examples further in this article.

custom-post-type-categories-info

Get Category of a Custom Post Type

To get categories of a custom post type and display them along with their respective posts, I will use the the_terms() function inside the loop.

the_terms() function display all the associated categories of a custom post type. It requires two parameters, post_id and taxonomy slug.

In the below code, $post->ID will automatically get the id of current post type and 'events-cat' is my taxonomy slug.

You can also add a prefix to the list of categories and separators between the categories by passing the value to their parameters. You can find the parameter reference here.

<?php 

// display assigned categories of a custom post type 

the_terms($post->ID, 'events-cat');

?>

Get Category Name & Link of a Custom Post Type

To get the category data like category name, URL of a custom post type, I will use get_the_terms() function inside the loop.

Again get_the_terms() function also required Post ID and taxonomy slug. This will return the custom post type’s category data as an array.

Now I will filter out the category data from this array by using foreach loop, and display my custom post type’s category name using $term->name, and category link using get_term_link($term) function. As shown in the code below.

<?php 
// displaying the category name of custom post type inside the loop 

$terms = get_the_terms($post->ID, 'events-cat');
			    		
foreach ($terms as $term) {

	echo '<li><a href="'.get_term_link($term).'">'.$term->name.'</a></li>';

}

?>

Get Custom Post Type Categories List

To get the list of all categories of my custom post type Events, I will use the get_terms() function by giving my custom post type’s taxonomy slug, which is 'events-cat' in my case.

You can also pass multiple taxonomies as an array to the taxonomy parameter.

Another important thing is, 'hide_empty' parameter, make sure it set to false. Otherwise it will not fetch the categories which has 0 post.

Don’t forget to replace the taxonomy slug with your custom post type taxonomy.

<?php

//Display a list of all the categories of custom post types

$terms = get_terms(
		 array(
				'taxonomy'   => 'events-cat', // Custom Post Type Taxonomy Slug
				'hide_empty' => false,
				'order'	     => 'asc'
			)
		 );

echo '<h2>List of all categories of custom post types</h2>';

echo '<ul>';

foreach ($terms as $term) {
		echo '<li><a href="'.get_term_link($term).'">'.$term->name.'</a></li>';
	}

echo '</ul>';

?>

Owner of diveinwp.com, solely responsible for creating helpful, informative content about WordPress, a fulltime WordPress developer with 8+ years of hands-on experience in WordPress design and development.