When it comes to creating a powerful and flexible website, WordPress, the leading content management system (CMS), has become the go-to platform for millions of users around the world.
With its vast array of features and plugins, WordPress allows you to customize every aspect of your site, including the URLs. Among the many customization features that WordPress offers, the ability to create custom taxonomy URLs stands out as a powerful tool.
Taxonomies are a fundamental aspect of organizing and categorizing content in WordPress. By default, WordPress provides built-in taxonomies like categories and tags. For more specialized websites, creating custom taxonomies is often necessary to effectively classify and manage content.
In this guide, we’ll explore custom taxonomy URLs and how you can leverage them to enhance your website’s structure and SEO.
Let’s jump in!
What is WordPress taxonomy?
Taxonomy refers to the practice of grouping your website content into logical and hierarchical classifications. Whether you run a personal blog, a corporate website, or an eCommerce platform, harnessing the potential of WordPress taxonomy can significantly enhance your content organization and improve user engagement.
It enables website owners and administrators to create custom structures that make content management and navigation a breeze, operating on a simple yet effective principle: dividing content into meaningful custom categories and tags.
Categories serve as broad classifications, while tags offer more specific descriptors. This hierarchical structure allows for better organization and easier retrieval of content, benefiting both website owners and visitors alike.
For instance, an online magazine may create taxonomies such as “topics”, “authors”, and “publication years” to efficiently categorize articles while a WooCommerce website could create taxonomies like “product types”, “brands”, and “price ranges” to help customers find products with ease. The possibilities are endless.
When you create a taxonomy, WordPress automatically generates URLs for each term within that taxonomy. These URLs typically follow a standardized format, such as example.com/taxonomy/term. While this structure is functional, it may not always align with your website’s design or SEO strategy.
Assuming you have a custom post type (recipe
) and a custom taxonomy (recipe_category
), a URL like this, /recipe/pizzas versus/recipe/recipe_category/pizzas
, looks unnecessarily long and complex. Google doesn’t like it either, which will drastically affect your search engine rankings.
Let’s face it, URLs look much nicer without the actual name of the taxonomy – that’s where custom taxonomy-based URLs come into play!
Use cases
For this tutorial, we are going to be using the following example use case to show you how to make your URLs easily readable and SEO-friendly.
On our website, we have a developers page that lists all our Codeable experts along with their respective tags, which they can use to highlight their skills, that has this structure:
/developers
– Paginated list of our developers/developers/javascript
– Paginated list of our developers that specialize in JavaScript. This is a taxonomy view./developers/tomaz-zaman
– Single view of a developer custom post type.
Note the similarity between the last two items. We need to tell WordPress to use the developers as a slug on both our custom post type (CPT) and our custom taxonomy.
Let’s see how we can fix that!
A step-by-step guide for creating custom WordPress taxonomy
To create custom taxonomy URLs, we’ll need to utilize a technique called Rewrite Rules, which will allow us to modify the default URL structure and define your own patterns.
By doing so, we will be able to achieve cleaner and more meaningful URLs that align with our website’s aesthetics and improve search engine optimization (SEO).
Step 1: Register a custom post type and a custom taxonomy
Before we can play around with rewriting our taxonomy URLs, we need to set up our entities using the register_taxonomy()
function in our child theme’s functions.php
file.
Within the registration code, you can define the desired rewrite rules for your taxonomy. This involves specifying the URL structure, including the base slug and any additional hierarchical or non-hierarchical elements.
Feel free to use your existing ones if you have them, but we’ll use the developer CPT because it’s a real use case we needed to implement. This is the code we are starting with:
function register_developer_entities() {
$developer_args = array(
'public' => true,
'label' => 'Developers',
'rewrite' => array( 'slug' => 'developers' ),
'taxonomies' => array( 'developer_tag' )
);
register_post_type( 'developer', $developer_args ); #post type comes first
$taxonomy_args = array(
'labels' => array( 'name' => 'Developer Tags' ),
'show_ui' => true,
'show_tagcloud' => false,
'rewrite' => array( 'slug' => 'developers' )
);
register_taxonomy( 'developer_tag', array( 'developer' ), $taxonomy_args );
}
add_action( 'init', 'register_developer_entities' );
This code is pretty straightforward, and we stripped most of the unnecessary parts out for the purpose of this tutorial. There are a few things you should notice, though:
- The order we register CPT and taxonomy – Taxonomy comes last; otherwise, it won’t work.
- The CPT and taxonomy both use the same slug (developers) – This can cause conflicts and lead to 404 errors if not handled properly.
- Improper URL rewriting – If you try to visit the URLs at this point, it may result in 404 errors, which are page not found errors. This will happen because we defined two separate entities with the same slug, and WordPress doesn’t know how to deal with that, so it overwrites one with the other.
The code snippet provided doesn’t include the URL rewriting part, so it needs to be implemented separately to ensure the correct routing of URLs.
Step 2: Generate rewrite rules
We need to manually append the URL for each of our custom terms to the rewrite rules, and this is how it’s done:
function generate_taxonomy_rewrite_rules( $wp_rewrite ) {
$rules = array();
$post_types = get_post_types( array( 'name' => 'developer', 'public' => true, '_builtin' => false ), 'objects' );
$taxonomies = get_taxonomies( array( 'name' => 'developer_tag', 'public' => true, '_builtin' => false ), 'objects' );
foreach ( $post_types as $post_type ) {
$post_type_name = $post_type->name; // 'developer'
$post_type_slug = $post_type->rewrite['slug']; // 'developers'
foreach ( $taxonomies as $taxonomy ) {
if ( $taxonomy->object_type[0] == $post_type_name ) {
$terms = get_categories( array( 'type' => $post_type_name, 'taxonomy' => $taxonomy->name, 'hide_empty' => 0 ) );
foreach ( $terms as $term ) {
$rules[$post_type_slug . '/' . $term->slug . '/?'] = 'index.php?' . $taxonomy->query_var . '=' . $term->slug;
}
}
}
}
$wp_rewrite->rules = $rules + $wp_rewrite->rules; // Add custom rules to existing rewrite rules
}
Let’s investigate what’s going on in this code:
- We created an empty array that will hold our custom rules, which we populated, and append to the default ones at the end of the function.
- We retrieved the custom post type and its matching taxonomy. This function also supports retrieving as many pairs as you want, hence the outer for-loop.
- The inner for-loop, though, is where the magic happens. We traversed through all the taxonomies in order to find a match between it and a corresponding post type. Once we do, we get all the terms for that taxonomy (in our case, that would be CSS, HTML, Gravity Forms, WooCommerce, etc.) and create a new rewrite rule for each term so that WordPress resolves our URL properly.
Once you have registered your custom taxonomy and defined the rewrite rules, you need to flush the rewrite rules for WordPress to recognize the changes.
This can be done by visiting the Permalinks settings page in your WordPress dashboard and simply clicking the Save Changes button.
This action refreshes the rewrite rules and ensures that your custom taxonomy URLs are generated correctly.
Step 3: Categorize information into taxonomy
Because we’re displaying two different entities under the same URL structure, we need to make sure WordPress renders them correctly.
They both need to display the same set of information. Taxonomy is, in a sense, just a filter, which is why we need to create a new template file in our WordPress child theme.
For our example, we will create taxonomy-developer_tag.php
and put the following content in it:
<?php
// Find and load the archive template.
locate_template( 'archive-developer.php', true );
This will just locate our developer archive template file and use that to display all developers that match a particular term.
Now that our custom taxonomy URLs are set up, we can enjoy the benefits they bring to our website.
Leverage custom taxonomy URLs in WordPress using Codeable
WordPress taxonomy is a powerful approach for organizing content on your website. Its flexible and intuitive nature empowers website owners to create custom structures that suit their specific needs. By implementing a well-thought-out taxonomy, you can enhance user experience, improve SEO performance, and simplify content management.
Don’t fret if this seems too complex – you can opt to hire one of our Codeable WordPress experts!
Codeable is an online platform that connects businesses and individuals in need of coding services with highly skilled developers.
You can submit your WordPress or WooCommerce projects through our website and receive proposals from qualified professionals. Then, you can review our developers’ profiles, ratings, and previous work to make an informed decision about whom to hire.
Pick from over 700+ professionals who specialize in creating custom taxonomy whether you run a wordPress blog or a WooCommere store.
Don’t miss out on the potential of custom WordPress taxonomy – submit your project to Codeable and unlock the benefits of efficient content organization, today!