Skip to content

Instantly share code, notes, and snippets.

@twentyfortysix
Last active February 13, 2016 19:48
Show Gist options
  • Save twentyfortysix/3c27bf1760920fb9ba68 to your computer and use it in GitHub Desktop.
Save twentyfortysix/3c27bf1760920fb9ba68 to your computer and use it in GitHub Desktop.
WP - register taxonomy
<?php
// hook into the init action and call create_my_taxonomies when it fires
add_action( 'init', 'create_my_taxonomies', 0 );
// create two taxonomies, hierarchival_group and liner_groups for the post type "my"
function create_my_taxonomies() {
// ------------ Hierarchical ------------
// Add new taxonomy, make it hierarchical (like categories)
$labels = array(
'name' => _x( 'hierarchical_groups', 'taxonomy general name' ),
'singular_name' => _x( 'hierarchical_group', 'taxonomy singular name' ),
'search_items' => __( 'Search hierarchical_groups' ),
'all_items' => __( 'All hierarchical_groups' ),
'parent_item' => __( 'Parent hierarchical_group' ),
'parent_item_colon' => __( 'Parent hierarchical_group:' ),
'edit_item' => __( 'Edit hierarchical_group' ),
'update_item' => __( 'Update hierarchical_group' ),
'add_new_item' => __( 'Add New hierarchical_group' ),
'new_item_name' => __( 'New hierarchical_group Name' ),
'menu_name' => __( 'hierarchical_group' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'hierarchical_group' ),
);
register_taxonomy( 'hierarchical_group', array( 'my' ), $args );
// ------------ LINEAR ------------
// Add new taxonomy, NOT hierarchical (like tags)
$labels = array(
'name' => _x( 'liner_groups', 'taxonomy general name' ),
'singular_name' => _x( 'liner_group', 'taxonomy singular name' ),
'search_items' => __( 'Search liner_groups' ),
'popular_items' => __( 'Popular liner_groups' ),
'all_items' => __( 'All liner_groups' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit liner_group' ),
'update_item' => __( 'Update liner_group' ),
'add_new_item' => __( 'Add New liner_group' ),
'new_item_name' => __( 'New liner_group Name' ),
'separate_items_with_commas' => __( 'Separate liner_groups with commas' ),
'add_or_remove_items' => __( 'Add or remove liner_groups' ),
'choose_from_most_used' => __( 'Choose from the most used liner_groups' ),
'not_found' => __( 'No liner_groups found.' ),
'menu_name' => __( 'liner_groups' ),
);
$args = 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' => 'liner_group' ),
);
register_taxonomy( 'liner_group', 'my', $args );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment