How to Add a Custom Taxonomy in WordPress

Creating a custom taxonomy in WordPress allows you to group your posts or custom post types in a specific way. This makes it easier for visitors to find related content and for you to manage your website’s content. In this article, we’ll go through the steps of creating a custom taxonomy in WordPress.

Step 1: Choose a Name for Your Taxonomy

The first step in creating a custom taxonomy is to choose a name for it. This name will be used to identify the taxonomy in WordPress. For example, if you were creating a custom taxonomy for a recipe website, you might choose the name “Ingredients”.

Step 2: Register Your Taxonomy

To register your custom taxonomy, you need to add some code to your functions.php file or a plugin. Using the function register_taxonomy here is an example of the code you could use to register a custom taxonomy called “Ingredients”:

function wpd_custom_taxonomy() {
  $labels = array(
    'name' => _x( 'Ingredients', 'taxonomy general name' ),
    'singular_name' => _x( 'Ingredient', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Ingredients' ),
    'all_items' => __( 'All Ingredients' ),
    'parent_item' => __( 'Parent Ingredient' ),
    'parent_item_colon' => __( 'Parent Ingredient:' ),
    'edit_item' => __( 'Edit Ingredient' ),
    'update_item' => __( 'Update Ingredient' ),
    'add_new_item' => __( 'Add New Ingredient' ),
    'new_item_name' => __( 'New Ingredient Name' ),
    'menu_name' => __( 'Ingredients' ),
  );

  register_taxonomy('wpd_ingredients',array('post','recipe'), array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'show_admin_column' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'ingredient' ),
  ));
}
add_action( 'init', 'wpd_custom_taxonomy', 0 );

In this code, we have defined the name of the taxonomy as “Ingredients” and set some labels for it. We have also defined the taxonomy as hierarchical, which means that it can have parent and child terms. We have then associated the taxonomy with the “post” and “recipe” post types.

Step 3: Add Terms to Your Taxonomy

Now that you have registered your custom taxonomy, you can start adding terms to it. Terms are the individual items that you will use to group your posts or custom post types. For example, if you were creating a custom taxonomy for a recipe website, you might add terms like “Beef”, “Chicken”, and “Fish”.

To add terms to your custom taxonomy, go to the WordPress dashboard and click on “Ingredients” (or whatever you named your custom taxonomy) under the “Posts” menu.

Step 4: Display Your Custom Taxonomy

Now that you have created your custom taxonomy and added some terms to it, you may want to display it on your website. To do this, you can use the WordPress get_the_terms() function.

Here is an example of how you could use the get_the_terms() function to display the terms for a custom post type called “Recipe”:

$terms = get_the_terms( $post->ID, 'wpd_ingredients' );
if ( $terms && ! is_wp_error( $terms ) ) {
	$ingredients_links = array();
	foreach ( $terms as $term ) {
		$term_link = get_term_link( $term );
		if ( is_wp_error( $term_link ) ) {
			continue;
		}
		$ingredients_links[] = '<a href="' . esc_url( $term_link ) . '">' . esc_html( $term->name ) . '</a>';
	}
	$ingredients = join( ', ', $ingredients_links );
	echo '<div class="ingredients">' . esc_html__( 'Ingredients: ', 'text-domain' ) . $ingredients . '</div>';
}

This code will display all the “Ingredients” custom taxonomy terms associated with a particular post. It first checks if the post has any “Ingredients” terms associated with it, and if so, it loops through them and creates a link to the term archive page. Finally, it displays all the “Ingredients” terms as a comma-separated list.

You can customize the HTML markup and CSS styles to fit your specific needs. This code can be used in various template files such as single post template, archive template, etc.

Congratulations! You have successfully created a custom taxonomy “Ingredients” and displayed it on your WordPress site.

Leave a Comment

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