In this post, we’ll define a taxonomy and teach you how to create one for your own website. Taxonomies are a simple yet effective approach to adding more functionality and flexibility to your website.
Custom taxonomies are the same as categories and tags. WordPress allows developers to create Custom Taxonomies. Custom Taxonomies are helpful when one wants to create distinct naming systems and make them accessible behind the scenes in a predictable way.
Create Custom Taxonomies by code.
Add the below code in the function.php file of your active child theme (or active theme).
1. Creating a Hierarchical Taxonomy
First, we create hierarchical taxonomy that works like categories and can have parent and child terms. Add the following code in your theme’s functions.php file or in a site-specific plugin (recommended) to create hierarchical custom taxonomy like categories:
<?php
add_action( 'init', 'cxc_create_course_hierarchical_taxonomy');
function cxc_create_course_hierarchical_taxonomy() {
$labels = array(
'name' => _x( 'Courses', 'taxonomy general name' ),
'singular_name' => _x( 'Course', 'taxonomy singular name' ),
'search_items' => __( 'Search Courses' ),
'all_items' => __( 'All Courses' ),
'parent_item' => __( 'Parent Course' ),
'parent_item_colon' => __( 'Parent Course:' ),
'edit_item' => __( 'Edit Course' ),
'update_item' => __( 'Update Course' ),
'add_new_item' => __( 'Add New Course' ),
'new_item_name' => __( 'New Course Name' ),
'menu_name' => __( 'Course' ),
);
$args = array(
'hierarchical' => true, // make it hierarchical (like categories)
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => [ 'slug' => 'course' ],
);
register_taxonomy( 'course', [ 'post' ], $args );
}
?>
2. Creating a Non-hierarchical Taxonomy
Create a non-hierarchical custom taxonomy like Tags, and add this code in your theme’s functions.php or in a site-specific plugin:
<?php
add_action( 'init', 'cxc_create_topics_taxonomy' );
function cxc_create_topics_taxonomy() {
$labels = array(
'name' => _x( 'Topics', 'taxonomy general name' ),
'singular_name' => _x( 'Topic', 'taxonomy singular name' ),
'search_items' => __( 'Search Topics' ),
'popular_items' => __( 'Popular Topics' ),
'all_items' => __( 'All Topics' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Topic' ),
'update_item' => __( 'Update Topic' ),
'add_new_item' => __( 'Add New Topic' ),
'new_item_name' => __( 'New Topic Name' ),
'separate_items_with_commas' => __( 'Separate topics with commas' ),
'add_or_remove_items' => __( 'Add or remove topics' ),
'choose_from_most_used' => __( 'Choose from the most used topics' ),
'menu_name' => __( 'Topics' ),
);
register_taxonomy('topics','books',array(
'hierarchical' => false,
'labels' => $labels,
'show_ui' => true,
'show_in_rest' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array( 'slug' => 'topic' ),
));
}
Take note of the differences between the two code pieces. The value for the hierarchical parameter in the recister taxonomy() function is set to true for category-like taxonomies and false for tags-like taxonomies.