0 votes
4.7k views
in Wordpress by
retagged
Can someone explains about register_taxonomy function in Wordpress. A few examples would be helpful if possible.

1 Answer

0 votes
by
In WordPress, you can create (or “register”) a new taxonomy by using the register_taxonomy() function.

/**

 * Add custom taxonomies

 */

function add_custom_taxonomies() {

  // Add new "Locations" taxonomy to Posts

  register_taxonomy('location', 'post', array(

    // Hierarchical taxonomy (like categories)

    'hierarchical' => true,

    // This array of options controls the labels displayed in the WordPress Admin UI

    'labels' => array(

      'name' => _x( 'Locations', 'taxonomy general name' ),

      'singular_name' => _x( 'Location', 'taxonomy singular name' ),

      'search_items' =>  __( 'Search Locations' ),

      'all_items' => __( 'All Locations' ),

      'parent_item' => __( 'Parent Location' ),

      'parent_item_colon' => __( 'Parent Location:' ),

      'edit_item' => __( 'Edit Location' ),

      'update_item' => __( 'Update Location' ),

      'add_new_item' => __( 'Add New Location' ),

      'new_item_name' => __( 'New Location Name' ),

      'menu_name' => __( 'Locations' ),

    ),

    // Control the slugs used for this taxonomy

    'rewrite' => array(

      'slug' => 'locations', // This controls the base slug that will display before each term

      'with_front' => false, // Don't display the category base before "/locations/"

      'hierarchical' => true // This will allow URL's like "/locations/boston/cambridge/"

    ),

  ));

}

add_action( 'init', 'add_custom_taxonomies', 0 );

After adding this to your theme’s functions.php file, you should see a new taxonomy under the “Posts” menu in the admin sidebar. It works just like categories but is separate and independent.

After adding a few terms to your new taxonomy, you can begin to organise the content in your posts by location. A new “Locations” box will appear to the right of your posts in the WordPress admin area. Use this the way you would categories.

Related questions

+2 votes
1 answer 2.1k views
0 votes
0 answers 3.0k views
0 votes
1 answer 839 views
+2 votes
2 answers 9.7k views
0 votes
1 answer 1.3k views
asked Aug 25, 2016 in Wordpress by Sita
0 votes
1 answer 1.9k views
...