When you create a Custom Post Type (such as Courses), relying solely on standard Categories and Tags often causes messy topic collisions. A Course belongs to a Department (e.g., Computer Science, Electrical Engineering) or an Academic Semester (Semester 1, Semester 6). WordPress allows developers to register tailored classification engines called Custom Taxonomies using register_taxonomy().
When registering a taxonomy, the 'hierarchical' argument dictates how the taxonomy behaves in the editor:
'hierarchical' => true): Functions exactly like Categories. Displays as a checklist in the editor and supports parent/child nested relationships (e.g. Faculty > Engineering > Computer Science).'hierarchical' => false): Functions like Tags. Displays as a comma-separated freeform text input box with autocomplete, best for flat keywords (e.g. PHP8, React, Linux).Attach a hierarchical department taxonomy to our course Custom Post Type:
// Register "Department" Taxonomy for "Course" CPT
add_action('init', 'veewom_register_department_taxonomy');
function veewom_register_department_taxonomy() {
$labels = array(
'name' => _x('Departments', 'taxonomy general name', 'veewom'),
'singular_name' => _x('Department', 'taxonomy singular name', 'veewom'),
'search_items' => __('Search Departments', 'veewom'),
'all_items' => __('All Departments', 'veewom'),
'parent_item' => __('Parent Department', 'veewom'),
'parent_item_colon' => __('Parent Department:', 'veewom'),
'edit_item' => __('Edit Department', 'veewom'),
'update_item' => __('Update Department', 'veewom'),
'add_new_item' => __('Add New Department', 'veewom'),
'new_item_name' => __('New Department Name', 'veewom'),
'menu_name' => __('Departments', 'veewom')
);
$args = array(
'hierarchical' => true, // true = Category style; false = Tag style
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true, // Adds a sortable column to wp-admin post list!
'query_var' => true,
'show_in_rest' => true, // Required for Gutenberg panel support!
'rewrite' => array('slug' => 'department', 'with_front' => false)
);
// Attach to 'course' post type (can pass array of multiple post types)
register_taxonomy('department', array('course'), $args);
}
Inside your theme's single-course.php template, display the attached department terms using get_the_term_list():
<?php
// Output formatted HTML list of attached departments
$departments = get_the_term_list( get_the_ID(), 'department', 'Department: ', ', ', '' );
if ( ! is_wp_error( $departments ) && ! empty( $departments ) ) {
echo '<div class="course-dept-badge">' . $departments . '</div>';
}
?>
When a visitor navigates to example.com/department/computer-science/, WordPress resolves the template in this exact order: 1) taxonomy-department-computer-science.php; 2) taxonomy-department.php; 3) taxonomy.php; 4) archive.php; 5) index.php.
Expand your CMS mastery by studying these tightly integrated WordPress development topics:
Build relational data models linking CPTs with multiple taxonomies.
Core concepts of taxonomy hierarchy, terms, and URL slugs.
Render terms using taxonomy-{taxonomy}-{term}.php templates.
Filter queries using advanced 'tax_query' relational parameters.