Tags

, , , ,

Create Custom Taxonomy Reuseable Function

Just paste this function in function.php

// Add new taxonomy
function taxoFunc($taxoName, $postType, $prefix){
	$menuName = ucwords($taxoName);
	$taxoName = strtolower(str_replace( ' ', '_', $taxoName));
	$labels = array(
		'name'              => _x( $menuName, 'taxonomy general name' ),
		'singular_name'     => _x( $menuName, 'taxonomy singular name' ),
		'search_items'      => __( 'Search' ),
		'all_items'         => __( 'All' ),
		'parent_item'       => __( 'Parent' ),
		'parent_item_colon' => __( 'Parent:' ),
		'edit_item'         => __( 'Edit' ),
		'update_item'       => __( 'Update' ),
		'add_new_item'      => __( 'Add New' ),
		'new_item_name'     => __( 'New Name' ),
		'menu_name'         => __( $menuName ),
	);

	$args = array(
		'hierarchical'      => true,
		'labels'            => $labels,	
		'show_ui'           => true,
		'show_admin_column' => true,
		'query_var'         => true,
		'rewrite'           => array( 'slug' => $taxoName, 'with_front' => false ),
	);

	register_taxonomy( $prefix.$taxoName, $postType, $args );
	
}

Now just place this line and change values according your post type

taxoFunc('Category', 'PostTypeName', 'c_');

You can call this function multiple time with different values…

Example:

taxoFunc('Category 1', 'PostTypeName', 'c_');
taxoFunc('Category 2', 'PostTypeName', 'c_');
taxoFunc('Category 3', 'PostTypeName', 'c_');
Advertisements