How do you create a custom taxonomy in WordPress?
To create a custom taxonomy in WordPress, follow these steps:
- Open the functions.php file in your WordPress theme's directory.
- Add the following code to register a new taxonomy:
function custom_taxonomy() {
$labels = array(
'name' => 'Taxonomy Name',
'singular_name' => 'Taxonomy Singular Name',
'menu_name' => 'Menu Name',
'all_items' => 'All Items',
'parent_item' => 'Parent Item',
'parent_item_colon' => 'Parent Item:',
'new_item_name' => 'New Item Name',
'add_new_item' => 'Add New Item',
'edit_item' => 'Edit Item',
'update_item' => 'Update Item',
'separate_items_with_commas' => 'Separate items with commas',
'search_items' => 'Search Items',
'add_or_remove_items' => 'Add or remove items',
'choose_from_most_used' => 'Choose from the considerable used items',
'not_found' => 'Not Found',
'no_terms' => 'No items',
'items_list_navigation' => 'Items list navigation',
'items_list' => 'Items list',
'back_to_items' => 'Back to items'
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'rewrite' => array( 'slug' => 'taxonomy' )
);
register_taxonomy( 'taxonomy', array( 'post' ), $args );
}
add_action( 'init', 'custom_taxonomy', 0 ); - Save the functions.php file.
- Go to your WordPress admin panel and click on 'Posts' or 'Pages', depending on the post type you assigned your taxonomy to.
- Click on 'Add New' or edit an existing post/page.
- In the right sidebar, you should now see a box labeled with the name of your taxonomy. You can add new terms by clicking the '+ Add New Taxonomy' link.
That's it! You have now created a custom taxonomy in WordPress.