If you’re a WordPress user, you’re probably familiar with the concept of taxonomies. Taxonomies are used to organize and categorize content on a WordPress site, and they can be especially useful for sites with a large amount of content.
While WordPress comes with a few default taxonomies (categories and tags), sometimes you may need to create your own WordPress custom taxonomies to better organize your content. In this article, we’ll be taking a look at how to create custom taxonomy in WordPress.
What are custom taxonomies?
Taxonomies are used to classify and organize content in WordPress. By default, WordPress has two main taxonomies: categories and tags. Categories are hierarchical and allow you to group your content into broad categories, while tags are non-hierarchical and allow you to add keywords to your content for easy searching.
Custom taxonomies, on the other hand, are taxonomies that you create to organize your content in a way that is more specific to your website’s needs. For example, if you have a recipe website, you may want to create a custom taxonomy called “Ingredients” to categorize your recipes by the main ingredient.
Why Create Custom Taxonomies?
Creating custom taxonomies in WordPress can be beneficial for several reasons. First and foremost, it allows you to organize your content in a more meaningful way. For example, if you have a recipe website, you could create a custom taxonomy called “Cuisine” to group recipes by the type of cuisine they belong to, such as Italian, Mexican, or Chinese. This would make it easier for users to find the recipes they’re interested in.
Custom taxonomies can also be used to create filters and sorting options on your website. For example, if you have an online store that sells clothing, you could create a custom taxonomy called “Size” to allow users to filter products by size.
Step-by-Step Guide to Creating Custom Taxonomies in WordPress
Creating a custom taxonomy in WordPress is a straightforward process. Here’s how to do it:
Step 1: Choose a name for your taxonomy
The first step is to choose a name for your taxonomy. This should be descriptive and easy to understand. For example, if you’re creating a taxonomy for different types of products, you could call it “Product Type.”
Step 2: Register your taxonomy
Once you’ve chosen a name, you need to register your taxonomy with WordPress. This can be done using the register_taxonomy() function. Here’s an example:
function custom_taxonomy() { $labels = array( 'name' => __( 'Product Type', 'textdomain' ), 'singular_name' => __( 'Product Type', 'textdomain' ), 'search_items' => __( 'Search Product Types', 'textdomain' ), 'all_items' => __( 'All Product Types', 'textdomain' ), 'parent_item' => __( 'Parent Product Type', 'textdomain' ), 'parent_item_colon' => __( 'Parent Product Type:', 'textdomain' ), 'edit_item' => __( 'Edit Product Type', 'textdomain' ), 'update_item' => __( 'Update Product Type', 'textdomain' ), 'add_new_item' => __( 'Add New Product Type', 'textdomain' ), 'new_item_name' => __( 'New Product Type Name', 'textdomain' ), 'menu_name' => __( 'Product Types', 'textdomain' ), ); $args = array( 'hierarchical' => true, 'labels' => $labels, 'show_ui' => true, 'show_admin_column' => true, 'query_var' => true, 'rewrite' => array( 'slug' => 'product-type' ), ); register_taxonomy( 'product-type', array( 'product' ), $args ); } add_action( 'init', 'custom_taxonomy', 0 );
you will need to replace “Product Type” with the name of your custom taxonomy. You can also customize the labels and other options to fit your needs.
In this example, we’re creating a custom taxonomy called “Product Type” and assigning it to the “product” post type. The $labels array contains labels used to describe the taxonomy in the WordPress admin area, while the $args
array contains various parameters that can be used to customize the taxonomy. Some of the most commonly used parameters include:
- hierarchical: Specifies whether the taxonomy should be hierarchical (like categories) or non-hierarchical (like tags).
- labels: Contains labels used to describe the taxonomy in the WordPress admin area.
- show_ui: Specifies whether the taxonomy should be visible in the WordPress admin area.
- show_admin_column: Specifies whether the taxonomy should be shown as a column in the WordPress admin area.
- query_var: Specifies whether the taxonomy should be used in URL queries.
- rewrite: Contains parameters used to customize the taxonomy’s URL structure.
Step 3: Customize your taxonomy
Once you’ve registered your taxonomy, you can customize it to fit your needs. There are several parameters you can adjust using the $args
array, including whether your taxonomy is hierarchical or not, the labels used to describe your taxonomy, and the way your taxonomy’s URL is structured.
- hierarchical: If set to true, the taxonomy will be hierarchical (like categories). If set to false, the taxonomy will be non-hierarchical (like tags).
- labels: This array contains labels used to describe the taxonomy in the WordPress admin area. You can customize these labels to better suit your needs.
- show_ui: If set to true, the taxonomy will be visible in the WordPress admin area. If set to false, it will be hidden.
- show_admin_column: If set to true, the taxonomy will be shown as a column in the WordPress admin area. If set to false, it will not be shown.
- query_var: If set to true, the taxonomy can be used in URL queries. If set to false, it cannot be used in URL queries.
- rewrite: This array contains parameters used to customize the taxonomy’s URL structure. You can use this parameter to change the URL structure of your taxonomy.
Step 4: Add terms to your taxonomy
Once you’ve customized your taxonomy, you can start adding terms to it. Terms are like categories or tags and are used to group similar content together. To add terms to your taxonomy, you can use the wp_insert_term()
function. Here’s an example:
$term = wp_insert_term( 'Shoes', // The name of the term 'product-type', // The slug of the taxonomy array( 'description' => 'This term represents shoes', 'slug' => 'shoes' ) );
In this example, we’re adding a term called “Shoes” to our custom taxonomy called “Product Type”. The wp_insert_term()
function returns an array containing information about the new term, including its ID, name, and slug. You can use this information to customize your taxonomy further or to display information about the term on your website.
Step 5: Assign your taxonomy to content
Finally, you need to assign your taxonomy to the content you want it to apply to. In our example, we want the “Product Type” taxonomy to apply to products. This can be done using the register_post_type()
function. Here’s an example:
// Our custom post type function function custom_post_type() { register_post_type( 'products', // CPT Options array( 'labels' => array( 'name' => __( 'Products', 'textdomain' ), 'singular_name' => __( 'Product', 'textdomain' ), ), 'public' => true, 'has_archive' => true, 'rewrite' => array( 'slug' => 'products' ), 'supports' => array( 'title', 'editor', 'thumbnail', 'custom-fields', 'excerpt' ), 'taxonomies' => array( 'product-type' ), ) ); } // Hooking up our function to theme setup add_action( 'init', 'custom_post_type' );
Step 6: Display the custom taxonomy on your website
Once you’ve created your custom taxonomy and added terms to it, you can start displaying it on your website. There are a few different ways to do this, depending on your needs.
One way to display your custom taxonomy is to use the get_terms()
function to retrieve a list of terms and then display them on your website. Here’s an example:
$terms = get_terms( array( 'taxonomy' => 'product-type', 'hide_empty' => false, ) ); foreach ( $terms as $term ) { echo '<a href="' . get_term_link( $term ) . '">' . $term->name . '</a><br>'; }
In this example, we’re using the get_terms()
function to retrieve a list of terms from our custom taxonomy called “Product Type”. We’re then using a foreach loop to loop through each term and display it on our website as a link.
Optimizing your custom taxonomy for SEO
Now that you have created your custom taxonomy, it’s important to optimize it for SEO. Here are a few tips:
- Use keywords in your custom taxonomy names and terms. This will help search engines understand what your content is about and improve your rankings.
- Use hierarchical structures for your custom taxonomies if appropriate. This can help organize your content and improve user experience.
- Use descriptive slugs for your custom taxonomy terms. This will make it easier for search engines to crawl and understand your content.
- Use your custom taxonomy in your website’s breadcrumbs. This will help users understand where they are on your website and improve navigation.
- Use your custom taxonomy in your website’s sitemap. This will help search engines crawl and index your content more effectively.
Conclusion
Creating custom taxonomies in WordPress can help you organize your content and improve your website’s SEO. By following the steps outlined in this article, you can create a custom taxonomy and optimize it for search engines. Remember to use descriptive terms, hierarchical structures, and keywords to improve your content’s visibility and help your website rank higher in search engine results pages.
Related Articles
How to get custom post data in WordPress?
Hello, guys do you want to learn how to get custom post type data in WordPress, then you are in the right post today in this article I will share with you a simple and fastest way to get custom post type data with many examples.
Read MoreHow to get WooCommerce product info?
When dealing with WooCommerce, we have to provide product information like the sku, product type, date, and many more in different locations. In that situation, we can use the code below to obtain any sort of product information.
Read More