Skip to content

Instantly share code, notes, and snippets.

@wpsmith
Created May 30, 2012 17:25
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wpsmith/2837775 to your computer and use it in GitHub Desktop.
Save wpsmith/2837775 to your computer and use it in GitHub Desktop.
Registering Multiple Taxonomies
<?php
/**
* Taxonomies
*
* This file registers any custom taxonomies
*
* @package WPS_Starter_Genesis_Child
* @author Travis Smith <travis@wpsmith.net>
* @copyright Copyright (c) 2012, Travis Smith
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
* @since 1.0.0
*/
/**
* Create Taxonomies
*
* @link http://codex.wordpress.org/Function_Reference/register_taxonomy
*
*/
function wps_create_taxonomies() {
$taxonomies = array(
'Department' => array ( 'post_types' => array( 'wps_employee' ) ),
'Skills' => array (
'post_types' => array( 'wps_employee' ),
'hierarchical' => false,
),
);
foreach ( $taxonomies as $title => $args ) {
wps_register_taxonomy( $title , $args );
}
}
add_action( 'init', 'wps_create_taxonomies' );
/**
* Registers a taxonomy with default values which can be overridden as needed.
*
* @author Travis Smith
*
* @uses sanitize_title() WordPress function that formats text for use as a slug
* @uses wp_parse_args() WordPress function that merges two arrays and parses the values to override defaults
* @uses register_taxonomy() WordPress function for registering a new taxonomy
*
* @param string $title title of the taxonomy. This will be automatically converted for plural and slug use
* @param array $args overrides the defaults
*/
function wps_register_taxonomy( $title, $args = array() , $prefix = 'wps_' ) {
$sanitizedTitle = sanitize_title( $title );
$plural_title = isset( $args['plural_title'] ) ? $args['plural_title'] : '';
unset ( $args['plural_title'] );
$defaults = array(
'hierarchical' => true,
'labels' => wps_taxonomy_labels( $title , $plural_title ),
'show_ui' => true,
'public' => true,
'query_var' => true,
'rewrite' => array( 'slug' => $sanitizedTitle ),
);
$args = wp_parse_args( $args, $defaults );
$taxonomy = isset( $args['taxonomy'] ) ? $args['taxonomy'] : $prefix . $sanitizedTitle;
$post_types = isset( $args['post_types'] ) ? $args['post_types'] : array();
register_taxonomy( $taxonomy , $post_types , $args );
}
// A helper function for generating the labels (taxonomy)
function wps_taxonomy_labels( $singular, $plural = '' ) {
if( $plural == '') $plural = $singular .'s';
return array(
'name' => __( $plural, CHILD_DOMAIN ),
'singular_name' => __( $singular, CHILD_DOMAIN ),
'search_items' => __( 'Search '. $plural , CHILD_DOMAIN ),
'all_items' => __( 'All '. $plural , CHILD_DOMAIN ),
'parent_item' => 'Parent ' . $singular,
'parent_item_colon' => 'Parent ' . $singular . ':',
'edit_item' => __( 'Edit '. $singular , CHILD_DOMAIN ),
'update_item' => __( 'Update ' . $singular, CHILD_DOMAIN ),
'add_new_item' => __( 'Add New ' . $singular, CHILD_DOMAIN ),
'new_item_name' => __( 'New ' . $singular . ' Name', CHILD_DOMAIN ),
'menu_name' => __( $singular , CHILD_DOMAIN ),
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment