How To Create Custom Taxonomies In WordPress
Not many people know what Taxonomy means in WordPress, even when they use the feature almost daily. Taxonomy in WordPress is a way of grouping posts (native and custom posts types) together in easily manageable groups. For WordPress users, the popular taxonomy options are Categories and Tags. In many cases, you might need to create custom taxonomies in WordPress websites to better organize your content.
In this tutorial, I will discuss how to:
- Create custom taxonomy
- Create non hierarchical custom taxonomy
- Display custom taxonomy
Create Custom Taxonomy
I will start by adding a taxonomy “Topics” in WordPress. For this, add the following code in the functions.php (located in the theme folder):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
add_action( 'init', 'create_cw_hierarchical_taxonomy', 0 ); //create a custom taxonomy name function create_cw_hierarchical_taxonomy() { $labels = array( 'name' => _x( 'Topics', 'taxonomy general name' ), 'singular_name' => _x( 'Topic', 'taxonomy singular name' ), 'search_items' => __( 'Search Topics' ), 'all_items' => __( 'All Topics' ), 'parent_item' => __( 'Parent Topic' ), 'parent_item_colon' => __( 'Parent Topic:' ), 'edit_item' => __( 'Edit Topic' ), 'update_item' => __( 'Update Topic' ), 'add_new_item' => __( 'Add New Topic' ), 'new_item_name' => __( 'New Topic Name' ), 'menu_name' => __( 'Topics' ), ); // taxonomy register register_taxonomy('topics',array('post'), array( 'hierarchical' => true, 'labels' => $labels, 'show_ui' => true, 'show_admin_column' => true, 'query_var' => true, 'rewrite' => array( 'slug' => 'topic' ), )); } |
Create Non-hierarchical Custom Taxonomy
Non-hierarchical taxonomy is distinct from the existing slug-based taxonomy of WordPress.
In the following code, you will notice “hierarchical=>false”. When used, this specifies metabox format WordPress also uses for Post Tags. however, in the case of “hierarchical=>false”, the metabox format is for WordPress categories.
Again, for creating non-hierarchical custom taxonomy options, add the following code in the functions.php (located in the theme folder)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
add_action( 'init', 'create_cw_nonhierarchical_taxonomy', 0 ); function create_cw_nonhierarchical_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' ), 'menu_name' => __( 'Topics' ), ); // Register non-hierarchical taxonomy register_taxonomy('topics','post',array( 'hierarchical' => false, 'labels' => $labels, 'show_ui' => true, 'show_admin_column' => true, 'update_count_callback' => '_update_post_term_count', 'query_var' => true, 'rewrite' => array( 'slug' => 'topic' ), )); } |
Display Custom Taxonomy
Once you have created custom taxonomy in WordPress, the next step is to display them on post pages. Fortunately, this is a matter of adding the following single line of code to the single.php (located in the theme folder):
1 |
the_terms( $post->ID, 'topics', 'Topics: ', ', ', ' ' ); |
By default custom taxonomies use the archive.php template to display posts. However, you can create a custom archive display for custom taxonomies by creating taxonomy-{taxonomy-slug}.php,where taxonomy-slug refers to slug of custom taxonomy.
Conclusion
In this tutorial I have discussed how to create custom taxonomies in WordPress manually. WordPress custom taxonomies is a great way of further categorizing the content on your website. If you need help with the issue, do leave a comment below.
Create Faster WordPress Websites!
Free eBook on WordPress Performance right in your inbox.