Introduction to Taxonomies in WordPress
Taxonomies are one of the most powerful features of WordPress, allowing you to organize your content effectively. In simple terms, a taxonomy is a method of classifying and grouping content. By default, WordPress includes tags and categories, but the platform also allows for the creation of customized taxonomies to suit the specific needs of each website.
In this article, we will explore how to create and manage custom taxonomies in WordPress, making it easier to organize content and improve the user experience.
What are Custom Taxonomies?
Custom taxonomies are tools that allow website developers and administrators to classify content in a way that best fits their business model or specific needs. For example, if you have a recipe site, you could create taxonomies such as "Type of Cooking" or "Difficulty", which would make it easier for users to find recipes that suit their preferences.
Some of the advantages of using custom taxonomies include:
- Effective organization: Improved site navigation.
- Content filtering: It allows users to find relevant content more easily.
- Optimized SEO: Improves content indexing in search engines.
Example of the Use of Custom Taxonomies
Let's imagine a book website. You could create taxonomies such as "Genre" (Fiction, Non-Fiction, Science Fiction) or "Author". This not only organizes the content, but also provides users with an intuitive way to navigate the site.
How to Create Custom Taxonomies in WordPress
To create custom taxonomies in WordPress, we use the `functions.php` file of our theme. Below is a step-by-step process for doing so.
Define Taxonomy
The first step is to use the `register_taxonomy()` function.
function create_taxonomy_genre() {
register_taxonomy(
'genre',
'movies',
array(
'label' => __( 'Genre' ),
'rewrite' => array( 'slug' => 'genre' ),
'hierarchical' => true,
)
);
}
add_action( 'init', 'create_taxonomy_gender' );
In this code, we are creating a taxonomy called "Genre" that will apply to the content type "movies". The `hierarchical` option allows the taxonomy to have a hierarchy structure similar to categories.
Taxonomy Options Configuration
The `register_taxonomy()` function accepts several parameters that you can adjust according to your needs. Below are some of the most common options:
- label: Name to be displayed in the administration panel.
- rewrite: Allows you to customize the taxonomy URL.
- hierarchical: Defines whether the taxonomy will be hierarchical or not.
It is advisable to play with these options to see which one best suits your use case.
Show Taxonomies in the Frontend
Once you have created your custom taxonomy, it is crucial to display it on the frontend so that users can interact with it. To do this, you can use the `get_the_terms()` function to get the taxonomy terms in your WordPress templates.
Taxonomy Display Code Example
Here is an example of how to display the "Genre" taxonomy in a movie template:
$generos = get_the_terms( get_the_ID(), 'genre' );
if ( $generos && ! is_wp_error( $generos ) ) {
echo '
- ‘;
- ' . esc_html( $gen->name ) . '
foreach ( $generos as $generos ) {
echo '
‘;
}
echo '
‘;
}
This code will retrieve and display all genres associated with a specific movie, making navigation easier for users.
Managing Custom Taxonomies in the Administration Panel
After creating your custom taxonomies, it is important to know how to manage them from the WordPress admin panel. You can add, edit or remove terms from your taxonomy through the corresponding section in the WordPress menu.
Access to Taxonomies Section
To access your custom taxonomy section, simply go to the administration menu and search for the name of your taxonomy. From there, you will be able to manage all the associated terms. It's an intuitive way to keep your content organized.
Conclusion
Creating custom taxonomies in WordPress is a great way to organize your content effectively. By following the steps outlined in this article, you can take your website structure to the next level, improving both user experience and search engine optimization.
