Site icon Experience Wordpress Developer Online – Design Develop and Maintenance Support

How To Create Custom Taxonomies In WordPress

We all know that in wordpress, tags and categories are taxonomies and tags are flat and categories are hierarchical. So Custom Taxonomies is a custom organizing system either flat or hierarchical that you create for your posts. So here we discuss how to create custom taxonomies in wordpress. Let’s have a look.

Add below line of code at the end of your theme’s functions.php file of your active child theme (or theme) and Save the file. Here we will create Hierarchical “Custom Taxonomy” Taxonomy in post of wordpress.

//create a custom taxonomy name
function wdo_create_hierarchical_taxonomy() {
$labels = array(
'name' => _x( 'Custom Taxonomy', 'taxonomy general name' ),
'singular_name' => _x( 'Custom Taxonomy', 'taxonomy singular name' ),
'search_items' => __( 'Search Custom Taxonomy' ),
'all_items' => __( 'All Custom Taxonomy' ),
'parent_item' => __( 'Parent Custom Taxonomy' ),
'parent_item_colon' => __( 'Parent Custom Taxonomy:' ),
'edit_item' => __( 'Edit Custom Taxonomy' ),
'update_item' => __( 'Update Custom Taxonomy' ),
'add_new_item' => __( 'Add New Custom Taxonomy' ),
'new_item_name' => __( 'New Custom Taxonomy Name' ),
'menu_name' => __( 'Custom Taxonomy' ),
);
// taxonomy register
register_taxonomy('customtaxonomy',array('post'), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'customtaxonomy' ),
));
}

add_action( 'init', 'wdo_create_hierarchical_taxonomy', 0 );

If you want to add Nonhierarchical Taxonomy than, add below line of code at the end of your theme’s functions.php file of your active child theme (or theme) and Save the file. Here we will create Nonhierarchical “Custom Taxonomy Nonhierarchical” Taxonomy in post of wordpress.

add_action( 'init', 'wdo_create_nonhierarchical_taxonomy', 0 );
function wdo_create_nonhierarchical_taxonomy() {
$labels = array(
'name' => _x( 'Custom Taxonomy Nonhierarchical', 'taxonomy general name' ),
'singular_name' => _x( 'Custom Taxonomy Nonhierarchical', 'taxonomy singular name' ),
'search_items' => __( 'Search Custom Taxonomy Nonhierarchical' ),
'popular_items' => __( 'Popular Custom Taxonomy Nonhierarchical' ),
'all_items' => __( 'All Custom Taxonomy Nonhierarchical' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Custom Taxonomy Nonhierarchical' ),
'update_item' => __( 'Update Custom Taxonomy Nonhierarchical' ),
'add_new_item' => __( 'Add New Custom Taxonomy Nonhierarchical' ),
'new_item_name' => __( 'New Custom Taxonomy Nonhierarchical Name' ),
'separate_items_with_commas' => __( 'Separate Custom Taxonomy Nonhierarchical with commas' ),
'add_or_remove_items' => __( 'Add or remove Custom Taxonomy Nonhierarchical' ),
'menu_name' => __( 'Custom Taxonomy Nonhierarchical' ),
);
// Register non-hierarchical taxonomy
register_taxonomy('customtaxonomynonhierarchical','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' => 'customtaxonomynonhierarchical' ),
));
}

After created custom taxonomyif you want to display them on post pages than add following line of code in your single.php file of your theme.

the_terms( $post->ID, 'customtaxonomy', 'Custom Taxonomy: ', ', ', ' ' );

You can add it in other files as well such as archive.php, index.php, and anywhere else you want to display the taxonomy.

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.

Exit mobile version