WordPress Taxonomies
Initially, what is a Taxonomy? It is a method for gathering things together. You may recollect terms from science class; living, organic entities are isolated into gatherings, for example, "kingdoms", "classes", "families", "variety", "species", etc. Each of these gatherings contains sub-bunches which assistance characterizes the pecking order of a solitary taxonomy. This makes the characterization and investigation of life on Earth simpler to oversee.
It's the same with WordPress, luckily they're significantly less unpredictable. In WordPress, taxonomies are utilized to help arrange content on your site with the goal that it is shown (and searchable) in an advantageous manner that bodes well.
Default WordPress Taxonomies
There are four built in taxonomies that you've probably used already:
- Category
- Tag
- Link Category
- Post Formats
Link Categories
Link Categories, is hidden for new installs and requires this plugin for them to function as normal. The 'link_category' taxonomy lets you categorize your links. You can use those only internally, for organizational reasons, and they are not showen on the site. They are usefull for defining groups of links to be displayed in sidebars.
Post Formats
WordPress 3.1 brings to us 'post_format' taxonomy. It's a piece of meta information and with it you can use a theme to customize its presentation of a post. To the default, existing ones you can't create or add new post formats.
Category
This taxonomy give you abillity to group posts together by sorting them into various categories. You can find these categories on the site by using /category/name types of URLs. Categories incline to be broad ranging and pre-defined.
Example:
Tag
The Tag taxonomy is just like categories, but more freeform. By simply typing them in, tags can be made up on the fly. You can find them on the site in the /tag/name types of URLs. They are generally displayed in the form of tag clouds or near posts and have numerous tags. Tags doesn't allow any hierarchical structure.
Example:
Custom WordPress Taxonomies
WordPress 2.3 has introduced the capability to create custom taxonomies. You can use two methods to create custom taxonomies.
Method 1
First, you have to install Simple Taxonomy and activate the plugin. Navigate to Settings » Custom Taxonomies and create a new taxonomy.
Give a name to taxonomy, which needs to be lowercase and with no special characters. Then you have to choose is this taxonomy will be hierarchical or not. If you rather to create a taxonomy like categories where you can add child term and a parent then choose True. Choose false if you want terms to be added like tags.
When you done this, you need to associate taxonomy with a post type. And finaly you have to choose none to 'Auto add terms automatically'.
Now, when you finish all the steps, tell WordPress how it should translate user interface for the topics.
Press the Add Taxonomy button and custom taxonomy will be created. It will have interface just like Categories or Tags (you can it under Posts). Also the custom taxonomy field will be in post edit area.
Method 2
This method is the 'code method' for those who prefer to do without a plugin.
To create a hierarchical custom taxonomy like categories, add the following code in a site-specific plugin (recommended) or in your theme’s functions.php file:
//hook into the init action and call create_book_taxonomies when it fires
add_action( 'init', 'create_topics_hierarchical_taxonomy', 0 );
//create a custom taxonomy name it topics for your posts
function create_topics_hierarchical_taxonomy() {
// Add new taxonomy, make it hierarchical like categories
//first do the translations part for GUI
$labels = array(
'name' => _x( 'Topics', 'taxonomy general name' ),
'singular_name' => _x( 'Topic', 'taxonomy singular name' ),
'search_items' => __( 'Search Topics' ),
'all_items' => __( 'All Topics' ),
'parent_item' => __( 'Parent Topic' ),
'parent_item_colon' => __( 'Parent Topic:' ),
'edit_item' => __( 'Edit Topic' ),
'update_item' => __( 'Update Topic' ),
'add_new_item' => __( 'Add New Topic' ),
'new_item_name' => __( 'New Topic Name' ),
'menu_name' => __( 'Topics' ), );
// Now register the taxonomy
register_taxonomy('topics',array('post'), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'topic' ),
));
}
Add this code in your theme’s functions.php or in a site-specific plugin to create a non-hierarchical custom taxonomy like Tags:
//hook into the init action and call create_topics_nonhierarchical_taxonomy when it fires
add_action( 'init', 'create_topics_nonhierarchical_taxonomy', 0 );
function create_topics_nonhierarchical_taxonomy() {
// Labels part for the GUI
$labels = array(
'name' => _x( 'Topics', 'taxonomy general name' ),
'singular_name' => _x( 'Topic', 'taxonomy singular name' ),
'search_items' => __( 'Search Topics' ),
'popular_items' => __( 'Popular Topics' ),
'all_items' => __( 'All Topics' ), 'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Topic' ),
'update_item' => __( 'Update Topic' ),
'add_new_item' => __( 'Add New Topic' ),
'new_item_name' => __( 'New Topic Name' ),
'separate_items_with_commas' => __( 'Separate topics with commas' ),
'add_or_remove_items' => __( 'Add or remove topics' ),
'choose_from_most_used' => __( 'Choose from the most used topics' ),
'menu_name' => __( 'Topics' ),
);
// Now register the non-hierarchical taxonomy like tag
register_taxonomy('topics','post',array(
'hierarchical' => false,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array( 'slug' => 'topic' ),
)
);
}
You can see difference between two codes. For hierarchical argument value is true, for category-like taxonomy but for tags-like taxonomies is false.
To display the terms you added to a custom taxonomy on your single post page you have to add this single line of code in your single.php file within the loop:
Your custom taxonomies, by default, use the archive.php template to display posts. But, you can create a custom archive display by creating taxonomy-{taxonomy-slug}.php.
Now you can create highly customized content management system (CMS) built to meet your needs by combining custom taxonomies with custom post types and custom meta boxes.