August 7, 2015
Create a custom post type (A-Z)
Let’s say I want a custom post type that has it’s own set of categories, instead of piggy-backing off the default “posts” categories. In essence it can be like having a completely separate posts section. We do this with CUSTOM POST TYPES and custom TAXONOMIES.
<?php
function codex_custom_init() {
register_post_type( 'events',
array(
'public' => true,
'has_archive' => true,
'menu_position' => 5, // PLACES NEW CUSTOM POST TYPE DIRECTLY BELOW 'POSTS' ON SIDE ADMIN NAV
'menu_icon' => '', // YOU CAN SET A CUSTOM ICON FOR THE SIDE MENU HERE
// THIS SETS UP ALL YOUR LABELS AND ADD/EDIT PROMPTS
'labels' => array(
'name' => __( 'Events' ),
'singular_name' => __( 'Event' ),
'add_new' => __( 'Add New' ),
'add_new_item' => __( 'Add New Event' ),
'edit' => __( 'Edit' ),
'edit_item' => __( 'Edit Event' ),
'new_item' => __( 'New Event' ),
'view' => __( 'View Event' ),
'view_item' => __( 'View Event' ),
'search_items' => __( 'Search Events' ),
'not_found' => __( 'No Events found' ),
'not_found_in_trash' => __( 'No Events found in Trash' ),
'parent' => __( 'Parent Event' ),
),
// YOU CAN REGISTER CUSTOM TAXONOMIES HERE
'taxonomies' => array('post_tag','event-type'),
// YOU CAN SPECIFY EXACTLY WHAT YOU WANT THE POST TYPE TO SUPPORT,
// MAKING IT SIMILAR TO PAIRED DOWN COMPARED TO REGULAR POSTS
'supports' => array(
'title','editor', 'thumbnail', 'excerpt', 'comments', 'custom-fields', 'tags')
)
);
}
// REGISTER CUSTOM TAXONOMIES
function event_init() {
// create a new taxonomy
$labels = array(
'name' => _x( 'Event Types', 'taxonomy general name' ),
'singular_name' => _x( 'Event Type', 'taxonomy singular name' ),
'search_items' => __( 'Search Event Types' ),
'all_items' => __( 'All Event Types' ),
'parent_item' => __( 'Parent Event Type' ),
'parent_item_colon' => __( 'Parent Event Type:' ),
'edit_item' => __( 'Edit Event Type' ),
'update_item' => __( 'Update Type' ),
'add_new_item' => __( 'Add New Event Type' ),
'new_item_name' => __( 'New Event Type Name' ),
'menu_name' => __( 'Event Types' ),
);
$args = array(
'menu_position' => 1,
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'event-type' ),
);
register_taxonomy('event-type',array('events'),$args);
}
// THEN YOU INITIALIZE THE CODE BY RUNNING THE "init" HOOK
add_action( 'init', 'codex_custom_init' );
add_action( 'init', 'event_init' );
?>
Custom post types