Today In this article, I will show you how to create custom taxonomies in WordPress Website And WooCommerce Website as well as how to display custom taxonomies in your WordPress theme.
What is a Taxonomy?
If you’re using WordPress as a blog, you’re already using taxonomies but you just may not know them by that name. A WordPress taxonomy is simply a system of coordinate information. A WordPress taxonomy, specifically, establishes WordPress posts. Your WordPress website has two taxonomies by default which they can be organized: Categories and Tags. I believe You have got an idea about WordPress Taxonomies. I just try to give a simple idea about the WordPress Taxonomies. If you believe that you did not get enough Ideas about WordPress Taxonomies, then read more here more details.
Creating Custom Taxonomies Using PHP Function
Add this code in your active theme functions.php file
//hook into the init action and call create_book_taxonomies when it fires add_action( 'init', 'create_topics_hierarchical_taxonomy', 0 ); //create a custom taxonomy name it topics for your posts function create_topics_hierarchical_taxonomy() { // Add new taxonomy, make it hierarchical like categories //first do the translations part for GUI $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' ), ); // Now register the taxonomy register_taxonomy('topics',array('post'), array( 'hierarchical' => true, 'labels' => $labels, 'show_ui' => true, 'show_admin_column' => true, 'query_var' => true, 'rewrite' => array( 'slug' => 'topic' ), )); }
To create a non-hierarchical custom taxonomy eg Tags, add this code in your active theme functions.php file.
//hook into the init action and call create_topics_nonhierarchical_taxonomy when it fires add_action( 'init', 'create_topics_nonhierarchical_taxonomy', 0 ); function create_topics_nonhierarchical_taxonomy() { // Labels part for the GUI $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' ), ); // Now register the non-hierarchical taxonomy like tag 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' ), )); }
Look at the difference between the two codes. The expenses for the hierarchical argument are True for category-like taxonomy and False for tags-like taxonomies. Also in the labels array for non-hierarchical taxonomy, I have added null for parent_item and parent_item_colon arguments.
Displaying Custom Taxonomies in WordPress Theme
Displaying you’re creating a custom taxonomy on your single post page. Add this code in your single.php file within the loop:
<?php the_terms( $post->ID, 'topics', 'Topics: ', ', ', ' ' ); ?>
Also, You can add this code to other files as well such as archive.php or index.php, and anywhere else you want to display the custom taxonomy.
Custom Taxonomies Using Free Plugin
Now, I would like to share with you how to create a Custom Taxonomies using a Free WordPress plugin. I would like to recommend this plugin for the Custom Taxonomies: Custom Post Type UI
Login Your WordPress Admin panel : To Plugin -> Add Plugin -> Search keyword Custom Post Type UI
Instill and active Custom Post Type UI plugin go to admin panel left menu and you will see one extra menu like CPT UI and click Add edit post type
Now, you can create custom post types and taxonomies for your website. Also, You can use the Plugin documentation for display taxonomies or how to use this plugin. I believe, this article would be helpful for creating a Custom Taxonomies.
Conclusion
I have tried to make the Custom Taxonomies a very easy way and I honestly believe if you follow my provided suggestion, you would be able to create Custom Taxonomies for your website. If Your need to speed up your website, check this article.
[…] this data to group or filter products. If you wish to filter by that type of data, then you want a custom taxonomy […]