Create Custom Taxonomies In WordPress
Taxonomies groups your content into specific categories. For example, you have tutorials on WordPress, WooCommerce and Drupal on your website. You can group them into topics like:
WordPress Tutorials
WooCommerce Tutorials
Drupal Tutorials
Creating a taxonomy on a WordPress website is easy. Just use the code below in your functions.php file and you are good to go:
addaction( 'init', 'createcwhierarchicaltaxonomy', 0 );
//create a custom taxonomy name
function createcwhierarchicaltaxonomy() {
$labels = array(
'name' => _x( 'Topics', 'taxonomy general name' ),
'singularname' => x( 'Topic', 'taxonomy singular name' ),
'searchitems' => _( 'Search Topics' ),
'allitems' => _( 'All Topics' ),
'parentitem' => _( 'Parent Topic' ),
'parentitemcolon' => _( 'Parent Topic:' ),
'edititem' => _( 'Edit Topic' ),
'updateitem' => _( 'Update Topic' ),
'addnewitem' => _( 'Add New Topic' ),
'newitemname' => _( 'New Topic Name' ),
'menuname' => _( 'Topics' ),
);
// taxonomy register
registertaxonomy('topics',array('post'), array(
'hierarchical' => true,
'labels' => $labels,
'showui' => true,
'showadmincolumn' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'topic' ),
));
}
I used the following reference articles to find the code. You can view the full guide from here as well.
https://www.wpblog.com/create-custom-taxonomies-in-wordpress/
http://www.wpbeginner.com/wp-tutorials/create-custom-taxonomies-wordpress/
Written by Moeez
Related protips
Have a fresh tip? Share with Coderwall community!
WordPress sites collect scripts from themes, plugins, and tag managers. A compromised plugin update can add a skimmer to every page without a change in your own templates.
Security insights from Cside