Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mor10
Created July 12, 2016 22:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save mor10/b532dfa732c4ea0c07643582dea1c0df to your computer and use it in GitHub Desktop.
Save mor10/b532dfa732c4ea0c07643582dea1c0df to your computer and use it in GitHub Desktop.
Create custom taxonomies (hierarchical and non-hierarchical) in a WordPress plugin
<?php
// Create two taxonomies, Class and Year, for the post type "Lecture"
function wpcampuscpt_lecture_taxonomies() {
// Add Class taxonomy, make it hierarchical (like categories)
$labels = array(
'name' => _x( 'Classs', 'taxonomy general name' ),
'singular_name' => _x( 'Class', 'taxonomy singular name' ),
'search_items' => __( 'Search Classes' ),
'all_items' => __( 'All Classes' ),
'parent_item' => __( 'Parent Class' ),
'parent_item_colon' => __( 'Parent Class:' ),
'edit_item' => __( 'Edit Class' ),
'update_item' => __( 'Update Class' ),
'add_new_item' => __( 'Add New Class' ),
'new_item_name' => __( 'New Class Name' ),
'menu_name' => __( 'Class' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'class' ),
);
register_taxonomy( 'class', array( 'lecture' ), $args );
// Add new Year taxonomy, make it non-hierarchical (like tags)
$labels = array(
'name' => _x( 'Years', 'taxonomy general name' ),
'singular_name' => _x( 'Year', 'taxonomy singular name' ),
'search_items' => __( 'Search Years' ),
'popular_items' => __( 'Popular Years' ),
'all_items' => __( 'All Years' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Year' ),
'update_item' => __( 'Update Year' ),
'add_new_item' => __( 'Add New Year' ),
'new_item_name' => __( 'New Year Name' ),
'separate_items_with_commas' => __( 'Separate years with commas' ),
'add_or_remove_items' => __( 'Add or remove years' ),
'choose_from_most_used' => __( 'Choose from the most used years' ),
'not_found' => __( 'No years found.' ),
'menu_name' => __( 'Years' ),
);
$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' => 'year' ),
);
register_taxonomy( 'year', 'lecture', $args );
}
add_action( 'init', 'wpcampuscpt_lecture_taxonomies', 0 );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment