CPT, Taxonomy | Type: PHPRegister Custom Post Types and Taxonomies

Example shows how to register a CPT and taxonomies.

if (!function_exists('sfcc_custom_posts')) {
// Register Custom Post Type
function sfcc_custom_posts() {

// OFFICES AND DEPARTMENTS
$labels = array(
	'name'                  => _x('Offices and Departments', 'Post Type General Name', 'sfcc'),
	'singular_name'         => _x('Office', 'Post Type Singular Name', 'sfcc'),
	'menu_name'             => __('Offices', 'sfcc'),
	'name_admin_bar'        => __('Offices', 'sfcc'),
	'archives'              => __('Office Archive', 'sfcc'),
	'parent_item_colon'     => __('Parent Item:', 'sfcc'),
	'all_items'             => __('All Offices', 'sfcc'),
	'add_new_item'          =>; __('Add New Office', 'sfcc'),
	'add_new'               => __('Add New', 'sfcc'),
	'new_item'              => __('New Office', 'sfcc'),
	'edit_item'             => __('Edit Office', 'sfcc'),
	'update_item'           => __('Update Office', 'sfcc'),
	'view_item'             => __('View Office', 'sfcc'),
	'search_items'          => __('Search Office', 'sfcc'),
	'not_found'             => __('Not found', 'sfcc'),
	'not_found_in_trash'    => __('Not offices found in Trash', 'sfcc'),
	'featured_image'        => __('Featured Image', 'sfcc'),
	'set_featured_image'    => __('Set featured image', 'sfcc'),
	'remove_featured_image' => __('Remove featured image', 'sfcc'),
	'use_featured_image'    => __('Use as featured image', 'sfcc'),
	'insert_into_item'      => __('Insert', 'sfcc'),
	'uploaded_to_this_item' => __('Uploaded to this item', 'sfcc'),
	'items_list'            => __('Items list', 'sfcc'),
	'items_list_navigation' => __('Items list navigation', 'sfcc'),
	'filter_items_list'     => __('Filter items list', 'sfcc'),
);
$args = array(
	'label'               => __('Office', 'sfcc'),
	'description'         => __('SFCC offices, academic schools departments, etc.', 'sfcc'),
	'labels'              => $labels,
	'supports'            => array('title', 'editor', 'thumbnail', 'revisions', 'custom-fields',),
	'taxonomies'          => array('office_category'),
	'hierarchical'        => TRUE,
	'public'              => TRUE,
	'show_ui'             => TRUE,
	'show_in_menu'        => TRUE,
	'menu_position'       => 20,
	'menu_icon'           => 'dashicons-groups',
	'show_in_admin_bar'   => TRUE,
	'show_in_nav_menus'   => TRUE,
	'can_export'          => TRUE,
	'has_archive'         => TRUE,
	'exclude_from_search' => FALSE,
	'publicly_queryable'  => TRUE,
	'capability_type'     => 'page',
);
register_post_type('office', $args);

add_action('init', 'sfcc_custom_posts', 0);
}

if (!function_exists('sfcc_taxonomies')) {
// Register Custom Taxonomies
function sfcc_taxonomies() {

register_taxonomy('person_category', array('person'), $args);
$labels = array(
	'name'                       => _x('Office Categories', 'Taxonomy General Name', 'sfcc'),
	'singular_name'              => _x('Office Category', 'Taxonomy Singular Name', 'sfcc'),
	'menu_name'                  => __('Office Category', 'sfcc'),
	'all_items'                  => __('All Office Categories', 'sfcc'),
	'parent_item'                => __('Parent Office Category', 'sfcc'),
	'parent_item_colon'          => __('Parent Office Category:', 'sfcc'),
	'new_item_name'              => __('New Office Category Name', 'sfcc'),
	'add_new_item'               => __('Add New Office Category', 'sfcc'),
	'edit_item'                  => __('Edit Office Category', 'sfcc'),
	'update_item'                => __('Update Office Category', 'sfcc'),
	'view_item'                  => __('View Office Category', 'sfcc'),
	'separate_items_with_commas' => __('Separate items with commas', 'sfcc'),
	'add_or_remove_items'        => __('Add or remove items', 'sfcc'),
	'choose_from_most_used'      => __('Choose from the most used', 'sfcc'),
	'popular_items'              => __('Popular Items', 'sfcc'),
	'search_items'               => __('Search Office Categories', 'sfcc'),
	'not_found'                  => __('Not Found', 'sfcc'),
	'no_terms'                   => __('No items', 'sfcc'),
	'items_list'                 => __('Items list', 'sfcc'),
	'items_list_navigation'      => __('Items list navigation', 'sfcc'),
);
$args = array(
	'labels'            => $labels,
	'hierarchical'      => TRUE,
	'public'            => TRUE,
	'show_ui'           => TRUE,
	'show_admin_column' => TRUE,
	'show_in_nav_menus' => TRUE,
	'show_tagcloud'     => TRUE,
	'rewrite'           => array(
		'slug'         => 'office-category',
		'with_front'   => TRUE,
		'hierarchical' => TRUE,
	),
);
}
register_taxonomy('office_category', array('office'), $args);
add_action('init', 'sfcc_taxonomies', 0);
}

// add categories for attachments
function sfcc_add_categories_for_attachments() {
	register_taxonomy_for_object_type('category', 'attachment');
}
add_action('init', 'sfcc_add_categories_for_attachments');

// add tags for attachments
function sfcc_add_tags_for_attachments() {
	register_taxonomy_for_object_type('post_tag', 'attachment');
}
add_action('init', 'sfcc_add_tags_for_attachments');

Reference Links

We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept