How To Register Custom Taxonomy Using Code

Are you looking to extend the way you can organize content on your WordPress website?

By creating custom taxonomies you can further customize the way your website’s visitors can sort your content.

What is Taxonomy in WordPress?

Taxonomy is a way to sort and organize the content on your WordPress website. By default, WordPress has two taxonomies – Categories and Tags.

While the default taxonomies are sufficient in most cases, sometimes they may not be enough. You can create and register custom taxonomies in those cases.

Registering custom taxonomies in WordPress 

While a plugin can make it easy for creating a custom taxonomy, it may not be the most optimal option. Below we are going to show how you can register a custom taxonomy using code.

/**

* Create custom taxonomies

* http://codex.wordpress.org/Function_Reference/register_taxonomy

*/

function create_custom_taxonomies() {

// Registering the taxonomy

register_taxonomy('products', 'post', array(

// If you want a hierarchical taxonomy like categories then set to true otherwise false for a taxonomy like tags

'hierarchical' => true,

// The names of the labels that will be displayed in the WordPress Admin dashboard

'labels' => array(

'name' => _x( Products', 'plural general name' ),

'singular_name' => _x( ’Product', 'singular name' ),

'search_items' => __( 'Search Products' ),

'all_items' => __( 'All Products' ),

'parent_item' => __( 'Parent Product' ),

'parent_item_colon' => __( 'Parent Product:' ),

'edit_item' => __( 'Edit Product' ),

'update_item' => __( 'Update Product' ),

'add_new_item' => __( 'Add New Product' ),

'new_item_name' => __( 'New Product Name' ),

'menu_name' => __( 'Products' ),

),

// Slugs details for this taxonomy

'rewrite' => array(

'slug' => 'products', // This is the base slug that will be displayed before each term

'with_front' => false, // Whether the category base  should be displayed before "/products/"

'hierarchical' => true // This allows the URLs like "/products/book/1984/"

),

));

}

// hook the function to the init action

add_action( 'init', create_custom_taxonomies, 0 );

Replace the products from the above code with your taxonomy name and labels.

Registering a non-hierarchical taxonomy

For a non-hierarchical taxonomy like tags, change the value of hierarchical argument to false in the code we shared above.

'hierarchical' => false

You will also need to set the parent_item and parent_item_colon to null.

'parent_item' => null,

'parent_item_colon' => null

That’s it. Your new taxonomy is non-hierarchical now.

Displaying custom taxonomies in your posts

After creating the custom taxonomy you will have to add some code to display the taxonomy in your posts. Depending on your theme this could be added to your single.php file or index.php. You can also add the code anywhere on your WordPress website and it will display the taxonomy terms.

<?php the_terms( $post->ID, products', 'Products: ', ', ', ' ' ); ?>

Replace the products with your custom taxonomy name.

The taxonomy page uses the archive.php template by default for design, you can create your own file and name it taxonomy-{taxonomy-slug}.php and then add the html code inside this page.

Have questions or confused about something WordPress Related? Join Our Forum & ask a Question

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top