How to Add Custom Taxonomies in WordPress Easily
Explanation
To add a custom taxonomy in WordPress, you can use the code provided to create a new category-like structure called "Genre". This is useful if you want to organize your posts into specific groups beyond the default categories and tags.
Here's what the code does:
- Registers a Custom Taxonomy: The function
wp_dudecom_register_custom_taxonomy
sets up a new taxonomy called "Genre". This is done by hooking into WordPress'sinit
action. - Labels: These are the names and descriptions you'll see in the WordPress admin area, like "Add New Genre" or "Edit Genre".
- Arguments: These settings control how the taxonomy behaves. For example, setting
hierarchical
totrue
makes it act like categories, allowing parent-child relationships. - Show in Admin: The
show_ui
andshow_admin_column
options make sure you can manage your genres in the WordPress dashboard. - Permalinks: The
rewrite
option with'slug' => 'genre'
determines the URL structure for your genres.
Adding to Menus:
- The function
wp_dudecom_add_custom_taxonomy_to_menu
adds your custom taxonomy to the WordPress menu. It checks if the menu location is 'primary' and then lists all the genres as menu items. - This is done using the
get_terms
function, which fetches all the terms (genres) you've created, and then adds them to the menu.
With this setup, you can easily categorize your posts using genres and even display them in your site's navigation menu, making it easier for visitors to find content based on these custom categories.
Code
<?php
// Hook into the 'init' action to register a custom taxonomy
add_action('init', 'wp_dudecom_register_custom_taxonomy');
/**
* Register a custom taxonomy called 'Genre'.
*
* This function creates a custom taxonomy for a custom post type or default post type.
* It is registered with various options to control its behavior.
*/
function wp_dudecom_register_custom_taxonomy() {
// Labels for the custom taxonomy
$labels = array(
'name' => _x('Genres', 'taxonomy general name', 'textdomain'),
'singular_name' => _x('Genre', 'taxonomy singular name', 'textdomain'),
'search_items' => __('Search Genres', 'textdomain'),
'all_items' => __('All Genres', 'textdomain'),
'parent_item' => __('Parent Genre', 'textdomain'),
'parent_item_colon' => __('Parent Genre:', 'textdomain'),
'edit_item' => __('Edit Genre', 'textdomain'),
'update_item' => __('Update Genre', 'textdomain'),
'add_new_item' => __('Add New Genre', 'textdomain'),
'new_item_name' => __('New Genre Name', 'textdomain'),
'menu_name' => __('Genre', 'textdomain'),
);
// Arguments for the custom taxonomy
$args = array(
'hierarchical' => true, // Set to true for categories, false for tags
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array('slug' => 'genre'),
);
// Register the taxonomy for the 'post' post type
register_taxonomy('genre', array('post'), $args);
}
/**
* Add custom taxonomy to WordPress menu.
*
* This function adds the custom taxonomy to the WordPress menu.
*/
add_filter('nav_menu_items_genre', 'wp_dudecom_add_custom_taxonomy_to_menu', 10, 2);
function wp_dudecom_add_custom_taxonomy_to_menu($items, $args) {
if ($args->theme_location == 'primary') {
$terms = get_terms(array(
'taxonomy' => 'genre',
'hide_empty' => false,
));
foreach ($terms as $term) {
$items .= '<li><a href="' . get_term_link($term) . '">' . $term->name . '</a></li>';
}
}
return $items;
}
?>
Instructions
File Location: Add the code to your theme's functions.php
file or a custom plugin file.
Prerequisites:
- Ensure you have access to your WordPress theme files or a custom plugin file.
- Backup your site before making changes to the code.
Implementation Steps:
- Access Your WordPress Files: Use an FTP client or your hosting provider's file manager to access your WordPress installation.
- Locate the
functions.php
File: Navigate towp-content/themes/your-theme-name/functions.php
or open your custom plugin file. - Edit the File: Open the
functions.php
file or plugin file in a text editor. - Paste the Code: Copy the provided code and paste it at the end of the file.
- Save Changes: Save the file and upload it back to your server if using FTP.
- Verify in WordPress Admin: Log in to your WordPress admin dashboard and navigate to Posts > Genres to see the new taxonomy.
- Add Genres to Menu: Go to Appearance > Menus and check if the genres appear in the menu structure under the 'primary' location.
With these steps, you have successfully added a custom taxonomy called "Genre" to your WordPress site, allowing you to organize posts and display them in your site's navigation menu.
If you need further assistance or want to explore more advanced functionalities, consider reaching out to wp-dude.com for expert WordPress support.