Introduction to WordPress Taxonomies
In WordPress, a taxonomy refers to a method for grouping posts and custom post types together. ‘Category’ and ‘Tag’ are frequently-used taxonomies that come built-in with WordPress. However, WordPress allows you to create new, custom taxonomies as well.
Creating Custom Taxonomies in WordPress
A custom taxonomy can be created using the register_taxonomy() function in WordPress. For detailed instructions on how to use this function, you can refer to the official WordPress documentation.
Here’s a simple way to use this function:
‘taxonomies’ => array(‘unique_slug’)
In the above code, you replace the ‘unique_slug’ with the slug of the taxonomy that you have created.
Creating Custom Category and Tag Taxonomies
I will now give you an example of creating custom taxonomies for categories and tags for a post type ‘composers’.
Custom Category Taxonomy
In the code snippet below, we’re creating a category-like taxonomy called ‘Composer Categories’:
function composers_custom_category() {
$labels = array(
‘name’ => x(‘Composer Categories’, ‘Taxonomy General Name’, ‘twentytwentyfour’), ‘singular_name’ => _x(‘Composer Category’, ‘Taxonomy Singular Name’, ‘twentytwentyfour’), ‘menu_name’ => _(‘Composer Categories’, ‘twentytwentyfour’),
// Other labels can be set here
);
$args = array(
‘labels’ => $labels,
‘hierarchical’ => true, // This makes it behave like categories
‘public’ => true,
‘show_ui’ => true,
‘show_admin_column’ => true,
‘show_in_nav_menus’ => true,
‘show_tagcloud’ => true,
‘show_in_rest’ => true,
‘rewrite’ => array(‘slug’ => ‘composer-category’),
);
register_taxonomy(‘composer_category’, array(‘composers’), $args);
}
add_action(‘init’, ‘composers_custom_category’, 0);
Custom Tag Taxonomy
Similar to categories, we can create a tag-like taxonomy. Here’s the code snippet for creating ‘Composer Tags’:
function composers_custom_tags() {
$labels = array(
‘name’ => x(‘Composer Tags’, ‘Taxonomy General Name’, ‘twentytwentyfour’), ‘singular_name’ => _x(‘Composer Tag’, ‘Taxonomy Singular Name’, ‘twentytwentyfour’), ‘menu_name’ => _(‘Composer Tags’, ‘twentytwentyfour’),
// Other labels can be set here
);
$args = array(
‘labels’ => $labels,
‘hierarchical’ => false, // This makes it behave like tags
‘public’ => true,
‘show_ui’ => true,
‘show_admin_column’ => true,
‘show_in_nav_menus’ => true,
‘show_tagcloud’ => true,
‘show_in_rest’ => true,
‘rewrite’ => array(‘slug’ => ‘composer-tag’),
);
register_taxonomy(‘composer_tag’, array(‘composers’), $args);
}
add_action(‘init’, ‘composers_custom_tags’, 0);
Recap
To summarize, WordPress allows you to create custom taxonomies—categories and tags alike—which give you the power to group your content in ways that make sense to you and your website visitors. You can create these taxonomies using WordPress’ inbuilt register_taxonomy() function and customize them as per your needs. Always remember, WordPress is all about customization and flexibility, so explore it to its fullest to create a website that best suits your requirements.