Pages

Sunday, November 17, 2019

How to Create Custom Taxonomies in WordPress

How to Create Custom Taxonomies in WordPress




WordPress is the most popular CMS in the web world and each month 400 million people visit the WordPress official website. WordPress reign 34% website of the internet in 2019 and a 4% increase from the previous year. WordPress.org offers 50,000 + plugins and over 3,500 GPL-licensed themes. So, if you are a WordPress theme developer then you may need to create custom post type or Custom Taxonomies for your theme. In this tutorial, I will share with you How to Create Custom Taxonomies for your WordPress theme. I will apply two ways that, How to Create Custom Taxonomies in WordPress manually (Using PHP functions) and other ways is to add Taxonomies in your active theme using WordPress Plugins. One Suggestion, if you are a beginner then I think first you should be known what is Taxonomies.

One Suggestion, if you are a beginner then I think first you should be known what is Taxonomies.

So, let's know, Understanding Taxonomies in WordPress.

If you’re using WordPress as a blog, you’re already using taxonomies but you just may not know them by that name. A taxonomy is simply a system of organizing information. A WordPress taxonomy, specifically, organizes WordPress posts. Your WordPress posts have two taxonomies by 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.

I am a WordPress and PHP expert so you can hire me for creating a WordPress website or WooCommerce website. Here my Fiverr Gig, so please check my Fiverr service details.



Manually Creating Custom Taxonomies Using PHP Functions. The Theme development part. Add the following code in your theme’s functions.php file.
/**
 * Following code add theme functions.php file. 
 * hook into the init action and call create_book_taxonomies when it fires
 * Add new taxonomy, make it hierarchical like categories
 * first do the translations part for GUI
 */

add_action( 'init', 'create_topics_hierarchical_taxonomy', 0 );
function create_topics_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' ),
 );  
// 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 theme functions.php.
/**
 * Following code add theme functions.php file. 
 * hook into the init action and call create_book_taxonomies when it fires
 * first do the translations part for GUI
 */


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' ),
  ));
}


You have already created WordPress Custom Taxonomies using PHP functions for your website. So now you should display Taxonomies, that you have created a few mins ago.

Here is you can display the custom taxonomy on your single post page. Add this single line of code in your single.php file within the loop. Also, You can add it in other files as well like archive.php, index.php, and anywhere else you want to display the taxonomy.
the_terms( $post->ID, 'topics', 'Topics: ', ', ', ' ' ); 

Now, if you want to add Custom Taxonomies for your website using WordPress free Plugins then I recommend this plugin. Check Please:(Custom Post Type UI )

I believe if you follow this tutorial, then you will able to create your own Custom Taxonomies for your website. Still, if you have any questions then please feel free to comment on the bellow or you can hire me for this job. Please check my Fiverr service. I will be very happy to work with you.