Skip to content

Instantly share code, notes, and snippets.

@jomurgel
Last active September 9, 2018 06:10
Show Gist options
  • Save jomurgel/68cba8be424aa2e45168788598bacfe5 to your computer and use it in GitHub Desktop.
Save jomurgel/68cba8be424aa2e45168788598bacfe5 to your computer and use it in GitHub Desktop.
Register multiple CPTs or Taxonomies in WordPress
<?php
/**
* Custom post types and/or taxonomies.
*
* @package Acorn Theme
*/
/**
* Register Custom Post Types
*/
function acorn_theme_register_custom_post_types() {
$custom_post_types = array(
array(
'slug' => __( 'custom-post-type', 'acorn' ),
'name' => __( 'Custom Post Type', 'acorn' ),
'short_name' => __( 'CPT', 'acorn' ),
'dashicon' => 'dashicons-editor-code',
),
);
foreach ( $custom_post_types as $cpt ) {
$labels = array(
'name' => $cpt['name'],
'singular_name' => $cpt['name'],
'menu_name' => $cpt['name'],
'name_admin_bar' => $cpt['name'],
'archives' => $cpt['name'] . __( ' Archives', 'acorn' ),
'attributes' => $cpt['name'] . __( ' Attributes', 'acorn' ),
'parent_item_colon' => __( 'Parent ', 'acorn' ) . $cpt['name'] . ':',
'all_items' => __( 'All ', 'acorn' ) . $cpt['name'],
'add_new_item' => __( 'Add ', 'acorn' ) . $cpt['short_name'],
'add_new' => __( 'Add New ', 'acorn' ) . $cpt['short_name'],
'new_item' => __( 'New ', 'acorn' ) . $cpt['short_name'],
'edit_item' => __( 'Edit ', 'acorn' ) . $cpt['short_name'],
'update_item' => __( 'Update ', 'acorn' ) . $cpt['short_name'],
'view_item' => __( 'View ', 'acorn' ) . $cpt['short_name'],
'view_items' => __( 'View ', 'acorn' ) . $cpt['short_name'],
'search_items' => __( 'Search ', 'acorn' ) . $cpt['short_name'],
'not_found' => __( 'Not found', 'acorn' ),
'not_found_in_trash' => __( 'Not found in Trash', 'acorn' ),
'featured_image' => __( 'Featured Image', 'acorn' ),
'set_featured_image' => __( 'Set featured image', 'acorn' ),
'remove_featured_image' => __( 'Remove featured image', 'acorn' ),
'use_featured_image' => __( 'Use as featured image', 'acorn' ),
'insert_into_item' => __( 'Insert into item', 'acorn' ),
'uploaded_to_this_item' => __( 'Uploaded to this item', 'acorn' ),
'items_list' => __( 'Items list', 'acorn' ),
'items_list_navigation' => __( 'Items list navigation', 'acorn' ),
'filter_items_list' => __( 'Filter items list', 'acorn' ),
);
$args = array(
'label' => $cpt['name'],
'description' => $cpt['name'] . __( ' projects', 'acorn' ),
'labels' => $labels,
'supports' => array( 'title', 'thumbnail', 'page-attributes', 'excerpt' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => $cpt['dashicon'],
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => true,
'publicly_queryable' => true,
'capability_type' => 'page',
'show_in_rest' => true,
);
register_post_type( $cpt['slug'], $args );
}
}
add_action( 'init', 'acorn_theme_register_custom_post_types', 0 );
<?php
/**
* Custom taxonomies
*
* @package Acorn Theme
*/
/**
* Register Custom Taxonomies
*/
function acorn_theme_register_taxonomies() {
$custom_taxonomy = array(
array(
'slug' => __( 'work-types', 'acorn' ),
'name' => __( 'Work Types', 'acorn' ),
'short_name' => __( 'Work Types', 'acorn' ),
'association' => array( 'work' ),
),
);
foreach ( $custom_taxonomy as $tax) {
// Register Custom Taxonomy
$labels = array(
'name' => $tax['name'],
'singular_name' => $tax['name'],
'menu_name' => $tax['name'],
'all_items' => __( 'All ', 'acorn' ) . $tax['name'],
'parent_item' => __( 'Parent ', 'acorn' ) . $tax['name'],
'parent_item_colon' => __( 'Parent ', 'acorn' ) . $tax['name'] . ':',
'new_item_name' => __( 'New ', 'acorn' ) . $tax['short_name'],
'add_new_item' => __( 'Add New ', 'acorn' ) . $tax['short_name'],
'edit_item' => __( 'Edit ', 'acorn' ) . $tax['short_name'],
'update_item' => __( 'Update ', 'acorn' ) . $tax['short_name'],
'view_item' => __( 'View ', 'acorn' ) . $tax['short_name'],
'separate_items_with_commas' => __( 'Separate items with commas', 'acorn' ),
'add_or_remove_items' => __( 'Add or remove items', 'acorn' ),
'choose_from_most_used' => __( 'Choose from the most used', 'acorn' ),
'popular_items' => __( 'Popular Items', 'acorn' ),
'search_items' => __( 'Search Items', 'acorn' ),
'not_found' => __( 'Not Found', 'acorn' ),
'no_terms' => __( 'No items', 'acorn' ),
'items_list' => __( 'Items list', 'acorn' ),
'items_list_navigation' => __( 'Items list navigation', 'acorn' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
);
register_taxonomy( $tax['slug'], $tax['association'], $args );
}
}
add_action( 'init', 'acorn_theme_register_taxonomies', 0 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment