How to Create Custom Taxonomies in WordPress

How to Create Custom Taxonomies in WordPress

Understanding Custom Taxonomies in WordPress

Custom taxonomies in WordPress offer a way to define and organize your content beyond the default categories and tags. By creating custom taxonomies, you can classify and structure content throughout your website more effectively. This guide will walk you through the process of creating custom taxonomies in WordPress.

What Are Custom Taxonomies?

A taxonomy in WordPress is a way to group posts and custom post types together. Default taxonomies include categories and tags. However, for greater flexibility, you can create your own custom taxonomies tailored to your content needs. For example, if you run a movie review site, you might create custom taxonomies for genres and actors.

Creating Custom Taxonomies

Using Plugins

The easiest way to create custom taxonomies is using a plugin. Custom Post Type UI is a popular choice. Follow these steps:

  1. Navigate to Plugins > Add New in your WordPress dashboard.
  2. Search for “Custom Post Type UI”, install, and activate the plugin.
  3. Go to CPT UI > Add/Edit Taxonomies.
  4. Fill out the fields to define your new taxonomy. This includes the name, slug, and the post types it will apply to.
  5. Click Add Taxonomy to save your settings.

For more plugin options, visit the WordPress Plugins Directory.

Manual Method via Code

If you prefer to add taxonomies manually, use the register_taxonomy() function. Here’s a basic example of code to add in your theme’s functions.php file:


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

    $args = array(
        'hierarchical'      => true,
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array('slug' => 'genre'),
    );

    register_taxonomy('genre', array('post'), $args);
}

add_action('init', 'create_movie_genre_taxonomy', 0);

This code snippet creates a hierarchical taxonomy called genre and applies it to the default post type. Customize the labels and array values to fit your specific needs.

Displaying Custom Taxonomies

To show custom taxonomies on your site, you can modify theme templates to fetch and display the taxonomy terms. Below is an example code snippet to list terms belonging to a specific taxonomy within a post loop:


$terms = get_the_terms(get_the_ID(), 'genre');
if ($terms && !is_wp_error($terms)) {
    $genre_list = array();
    foreach ($terms as $term) {
        $genre_list[] = $term->name;
    }
    echo implode(', ', $genre_list);
}

In the above code, fetching and listing the genre associated with each post adds clarity to the content presentation. Explore WordPress documentation on taxonomy templates for deeper insights.

Styling and Customization

After creating and displaying taxonomies, consider styling them to blend seamlessly with your existing design. Use CSS classes or custom-built options to style taxonomy term lists, providing a coherent look and enhancing the UX.

Additionally, ensure your taxonomy terms relate well to existing categories, maintaining a logical and navigable structure.

Conclusion

By creating custom taxonomies, you can better organize your WordPress content and enhance your site’s navigation and usability. Whether using a plugin for simplicity or coding directly for more control, custom taxonomies allow you to tailor the content classification to fit your unique website requirements.