Custom Taxonomy Implementation
Basic Solution
Here's a code snippet to create a custom taxonomy. Place this in your theme's functions.php
file or in a site-specific plugin:
This code creates a custom taxonomy called "Genre" for posts:
function create_genre_taxonomy() {
register_taxonomy(
'genre',
'post',
array(
'labels' => array(
'name' => 'Genres',
'singular_name' => 'Genre',
'menu_name' => 'Genres',
'add_new_item' => 'Add New Genre',
'new_item_name' => 'New Genre Name'
),
'hierarchical' => true,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array('slug' => 'genre'),
'show_in_rest' => true
)
);
}
add_action('init', 'create_genre_taxonomy');
Advanced Solution
This extended version creates a custom taxonomy for multiple post types with additional features:
function register_custom_taxonomies() {
// Book Genre Taxonomy
$labels = array(
'name' => 'Genres',
'singular_name' => 'Genre',
'menu_name' => 'Genres',
'all_items' => 'All Genres',
'edit_item' => 'Edit Genre',
'view_item' => 'View Genre',
'update_item' => 'Update Genre',
'add_new_item' => 'Add New Genre',
'new_item_name' => 'New Genre Name',
'search_items' => 'Search Genres',
'popular_items' => 'Popular Genres',
'separate_items_with_commas' => 'Separate genres with commas',
'choose_from_most_used' => 'Choose from most used genres',
'not_found' => 'No genres found'
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'show_in_rest' => true,
'query_var' => true,
'rewrite' => array('slug' => 'genre'),
'capabilities' => array(
'manage_terms' => 'manage_categories',
'edit_terms' => 'manage_categories',
'delete_terms' => 'manage_categories',
'assign_terms' => 'edit_posts'
)
);
register_taxonomy('genre', array('post', 'book'), $args);
}
add_action('init', 'register_custom_taxonomies');
Usage Notes
- Replace 'genre' with your desired taxonomy name
- Modify 'post' or array('post', 'book') to specify which post types should use this taxonomy
- Set 'hierarchical' to true for category-like behavior or false for tag-like behavior
- 'show_in_rest' enables Gutenberg editor support
Plugin Alternatives
If you prefer a plugin solution:
-
Custom Post Type UI - User-friendly interface for creating custom taxonomies
-
Pods - Advanced content type and field management
-
Toolset Types - Professional solution for custom content structures
Important Considerations
- After creating a new taxonomy, visit Settings > Permalinks to refresh rewrite rules
- Custom taxonomies can improve SEO by creating more specific content organization
- Use meaningful taxonomy names that reflect your content structure
- Consider URL structure when setting the 'rewrite' slug