What do taxonomies mean in WordPress?
When it comes to WordPress, taxonomies play an important role in the user experience. Taxonomies are methods of classifying content to make it easier to find. Some types of taxonomies that exist by default are the categories and tags used for blog posts. Even products in an online store are classified into such categories and labels.
How can you create new taxonomies?
To illustrate how you can create a new selection category, we will present a code that will generate a new color taxonomy (by which we can later assign each color a color to which it belongs).
// Register Custom Taxonomy
function custom_taxonomy() {
$labels = array(
'name' => _x( 'Colors', 'Taxonomy General Name', 'text_domain' ),
'singular_name' => _x( 'Color', 'Taxonomy Singular Name', 'text_domain' ),
'menu_name' => __( 'Colors', 'text_domain' ),
'all_items' => __( 'All colors', 'text_domain' ),
'parent_item' => __( 'Parent Item', 'text_domain' ),
'parent_item_colon' => __( 'Parent Item:', 'text_domain' ),
'new_item_name' => __( 'new color', 'text_domain' ),
'add_new_item' => __( 'edit color', 'text_domain' ),
'edit_item' => __( 'editing color', 'text_domain' ),
'update_item' => __( 'Update color', 'text_domain' ),
'view_item' => __( 'see color', 'text_domain' ),
'separate_items_with_commas' => __( 'Separate items with commas', 'text_domain' ),
'add_or_remove_items' => __( 'add or remove items', 'text_domain' ),
'choose_from_most_used' => __( 'choose_from_most_used', 'text_domain' ),
'popular_items' => __( 'Populars colors', 'text_domain' ),
'search_items' => __( 'search colors', 'text_domain' ),
'not_found' => __( 'Not Found', 'text_domain' ),
'no_terms' => __( 'No items', 'text_domain' ),
'items_list' => __( 'color list', 'text_domain' ),
'items_list_navigation' => __( 'Items list navigation', 'text_domain' ),
);
$rewrite = array(
'slug' => 'color',
'with_front' => true,
'hierarchical' => false,
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'rewrite' => $rewrite,
);
register_taxonomy( 'Culori', array( 'post' ), $args );
}
add_action( 'init', 'custom_taxonomy', 0 );
Brief code explanation:
- A function is created in the function.php file (we called it custom_taxonomy () but it can have any name you want).
- Within the created function, 3 arrays will be created as follows:
– $ labels – as the name implies, sets the labels that will appear in the WordPress menu of the control panel.
– $ rewrite – hierarchy specifications and more.
– $ args – mentions, among other things, who the category slug should be (will appear in the category URL) - After the 3 arrays, the new taxonomies are registered by the default function register_taxonomy () which contains:
– The name
– Purpose of use
– $ args.
In order to activate the taxonomies, we will use a WordPress action hook, respectively add_action () which will contain at least two arguments: the first will tell what type of action is invoked (in our case “init”) and the second will specify the function in which taxonomies were created.
To customize your own taxonomies, you can use the above code as a reference, replacing the words “color / colors” with the name you want to enter, using the singular and plural of the name where necessary.