Skip to content

Instantly share code, notes, and snippets.

@Galibri
Last active October 2, 2019 18:18
Show Gist options
  • Save Galibri/ca13cf46cf9a7fbdf5d0f75dede7e0d6 to your computer and use it in GitHub Desktop.
Save Galibri/ca13cf46cf9a7fbdf5d0f75dede7e0d6 to your computer and use it in GitHub Desktop.
Create Taxonomy for WordPress Custom/Built-in post type
<?php
// hook into the init action and call bs_create_color_taxonomy when it fires
add_action( 'init', 'bs_create_color_taxonomy', 0 );
function bs_create_color_taxonomy() {
// Add new taxonomy, make it hierarchical (like categories)
$labels = array(
'name' => _x( 'Colors', 'taxonomy general name', 'textdomain' ),
'singular_name' => _x( 'Color', 'taxonomy singular name', 'textdomain' ),
'search_items' => __( 'Search Colors', 'textdomain' ),
'all_items' => __( 'All Colors', 'textdomain' ),
'parent_item' => __( 'Parent Color', 'textdomain' ),
'parent_item_colon' => __( 'Parent Color:', 'textdomain' ),
'edit_item' => __( 'Edit Color', 'textdomain' ),
'update_item' => __( 'Update Color', 'textdomain' ),
'add_new_item' => __( 'Add New Color', 'textdomain' ),
'new_item_name' => __( 'New Color Name', 'textdomain' ),
'menu_name' => __( 'Color', 'textdomain' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'color' ),
);
register_taxonomy( 'color', array( 'product' ), $args );
}
<?php
//simple taxonomy creation
add_action( 'init', 'bs_create_product_color_tax' );
function bs_create_product_color_tax() {
register_taxonomy(
'color',
'product',
array(
'label' => __( 'Color' ),
'rewrite' => array( 'slug' => 'color' ),
'hierarchical' => true,
)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment