4.1 - Creating basic custom taxonomies
In plugins/posttypes.php:
// Custom Taxonomies
function my_custom_taxonomies() {
register_taxonomy(
'Type of Product/Service',
'review',
array(
'label' => __( 'Type of Product/Service' ),
'rewrite' => array( 'slug' => 'product-type' ),
'hierarchical' => true,
)
);
register_taxonomy(
'Price Range',
'review',
array(
'label' => __( 'Price Range' ),
'rewrite' => array( 'slug' => 'price' ),
'hierarchical' => true,
)
);
register_taxonomy(
'Mood',
'review',
array(
'label' => __( 'Mood' ),
'rewrite' => array( 'slug' => 'mood' ),
'hierarchical' => false,
)
);
}
add_action( 'init', 'my_custom_taxonomies' );
4.3 - Building out advanced Custom Taxonomies
In plugins/posttypes.php:
// Custom Taxonomies
function my_custom_taxonomies() {
// Type of Product/Service taxonomy
$labels = array(
'name' => 'Type of Products/Services',
'singular_name' => 'Type of Product/Service',
'search_items' => 'Search Types of Products/Services',
'all_items' => 'All Types of Products/Services',
'parent_item' => 'Parent Type of Product/Service',
'parent_item_colon' => 'Parent Type of Product/Service:',
'edit_item' => 'Edit Type of Product/Service',
'update_item' => 'Update Type of Product/Service',
'add_new_item' => 'Add New Type of Product/Service',
'new_item_name' => 'New Type of Product/Service Name',
'menu_name' => 'Type of Product/Service',
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'product-types' ),
);
register_taxonomy( 'product-type', array( 'review' ), $args );
// Mood taxonomy (non-hierarchical)
$labels = array(
'name' => 'Moods',
'singular_name' => 'Mood',
'search_items' => 'Search Moods',
'popular_items' => 'Popular Moods',
'all_items' => 'All Moods',
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => 'Edit Mood',
'update_item' => 'Update Mood',
'add_new_item' => 'Add New Mood',
'new_item_name' => 'New Mood Name',
'separate_items_with_commas' => 'Separate moods with commas',
'add_or_remove_items' => 'Add or remove moods',
'choose_from_most_used' => 'Choose from the most used moods',
'not_found' => 'No moods found.',
'menu_name' => 'Moods',
);
$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' => 'moods' ),
);
register_taxonomy( 'mood', array( 'review', 'post' ), $args );
}
add_action( 'init', 'my_custom_taxonomies' );
4.5 - Solution: Create a Custom Taxonomy for Rating
In plugins/posttypes.php:
// Price Range taxonomy
$labels = array(
'name' => 'Price Ranges',
'singular_name' => 'Price Range',
'search_items' => 'Search Price Ranges',
'all_items' => 'All Price Ranges',
'parent_item' => 'Parent Price Range',
'parent_item_colon' => 'Parent Price Range:',
'edit_item' => 'Edit Price Range',
'update_item' => 'Update Price Range',
'add_new_item' => 'Add New Price Range',
'new_item_name' => 'New Price Range Name',
'menu_name' => 'Price Range',
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'prices' ),
);
register_taxonomy( 'price', array( 'review' ), $args );