Home » Labs » How to create custom taxonomies in WordPress  

How to create custom taxonomies in WordPress


Filed under Labs

Custom taxonomies are a core feature introduced in WordPress ver.3 and 3.1 included many enhancements to support custom taxonomies. There are four built-in taxonomies in WordPress which are Categories,Tags,Links and Navigation menu respectively.

Besides that creating your own custom taxonomies will allow you to group content in a new way. In WordPress you can create /register a new taxonomy by using the register_taxonomy() function. Here is simple example code for creating custom taxonomies. Copy below code to your functions.php of your wordpress theme and it will create custom taxonomies.

 
<?php
 
//create a function that will attach our new 'member' taxonomy to the 'post' post type
function add_member_taxonomy_to_post(){
 
    //set the name of the taxonomy
    $taxonomy = 'member';
    //set the post types for the taxonomy
    $object_type = 'post';
 
    //populate our array of names for our taxonomy
    $labels = array(
        'name'               => 'Members',
        'singular_name'      => 'Member',
        'search_items'       => 'Search Members',
        'all_items'          => 'All Members',
        'parent_item'        => 'Parent Member',
        'parent_item_colon'  => 'Parent Member:',
        'update_item'        => 'Update Member',
        'edit_item'          => 'Edit Member',
        'add_new_item'       => 'Add New Member', 
        'new_item_name'      => 'New Member Name',
        'menu_name'          => 'Member'
    );
 
    //define arguments to be used 
    $args = array(
        'labels'            => $labels,
        'hierarchical'      => true,
        'show_ui'           => true,
        'how_in_nav_menus'  => true,
        'public'            => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array('slug' => 'member')
    );
 
    //call the register_taxonomy function
    register_taxonomy($taxonomy, $object_type, $args); 
}
add_action('init','add_member_taxonomy_to_post'); 
 
?>

You can read more at here

It's only fair to share...Share on Google+Tweet about this on TwitterShare on Facebook

Posts you may like: