Skip to content

Instantly share code, notes, and snippets.

@billerickson
Last active December 3, 2022 06:05
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save billerickson/74c7262b5dcee23c5ef2 to your computer and use it in GitHub Desktop.
Save billerickson/74c7262b5dcee23c5ef2 to your computer and use it in GitHub Desktop.
<?php
/**
* Register Multiple Taxonomies
*
* @author Bill Erickson
* @link http://www.billerickson.net/code/register-multiple-taxonomies/
*/
function be_register_taxonomies() {
$taxonomies = array(
array(
'slug' => 'job-department',
'single_name' => 'Department',
'plural_name' => 'Departments',
'post_type' => 'jobs',
'rewrite' => array( 'slug' => 'department' ),
),
array(
'slug' => 'job-type',
'single_name' => 'Type',
'plural_name' => 'Types',
'post_type' => 'jobs',
'hierarchical' => false,
),
array(
'slug' => 'job-experience',
'single_name' => 'Min-Experience',
'plural_name' => 'Min-Experiences',
'post_type' => 'jobs',
),
);
foreach( $taxonomies as $taxonomy ) {
$labels = array(
'name' => $taxonomy['plural_name'],
'singular_name' => $taxonomy['single_name'],
'search_items' => 'Search ' . $taxonomy['plural_name'],
'all_items' => 'All ' . $taxonomy['plural_name'],
'parent_item' => 'Parent ' . $taxonomy['single_name'],
'parent_item_colon' => 'Parent ' . $taxonomy['single_name'] . ':',
'edit_item' => 'Edit ' . $taxonomy['single_name'],
'update_item' => 'Update ' . $taxonomy['single_name'],
'add_new_item' => 'Add New ' . $taxonomy['single_name'],
'new_item_name' => 'New ' . $taxonomy['single_name'] . ' Name',
'menu_name' => $taxonomy['plural_name']
);
$rewrite = isset( $taxonomy['rewrite'] ) ? $taxonomy['rewrite'] : array( 'slug' => $taxonomy['slug'] );
$hierarchical = isset( $taxonomy['hierarchical'] ) ? $taxonomy['hierarchical'] : true;
register_taxonomy( $taxonomy['slug'], $taxonomy['post_type'], array(
'hierarchical' => $hierarchical,
'labels' => $labels,
'show_ui' => true,
'query_var' => true,
'rewrite' => $rewrite,
));
}
}
add_action( 'init', 'be_register_taxonomies' );
@just-us
Copy link

just-us commented Sep 1, 2017

Just wondering, does the first section have to have the same name as the function called? Apologies if this is a ridiculous question, just trying to get a better understanding of PHP

taxonomies() $taxonomies

@harshclimate
Copy link

BE - How come there are certain taxonomies that are hierarchical and some that aren't? Are those the differences between a "category" and a "tag"? Also, I'm assuming that if i'm adding in more taxonomies, I'm just going to copy the array's above and the code below will account for that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment