Skip to content

Instantly share code, notes, and snippets.

@tessak22
Created December 10, 2017 17:46
Show Gist options
  • Save tessak22/290629a44d300c27c167bd85b8840824 to your computer and use it in GitHub Desktop.
Save tessak22/290629a44d300c27c167bd85b8840824 to your computer and use it in GitHub Desktop.
Create Custom Post Type in WordPress
add_action('init', 'tessak22_register_custom_post_types', 0);
function tessak22_register_custom_post_types()
{
$arr_custom_post_type_options = array(
/*
array(
'label' => 'lowercase_name' // ** 20 char max, no spaces or caps
'singlar' => 'Human-Readable Item' // singular name
'plural' => 'Human-Readable Items' // plural name
'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'page-attributes', 'post-formats')
),
'icon' => 'dashicons-groups',
*/
array(
'label' => 'talks',
'singular' => 'Talk',
'plural' => 'Talks',
'supports' => array('title', 'editor', 'custom-fields', 'page-attributes','excerpt','thumbnail'),
'icon' => 'dashicons-megaphone',
),
array(
'label' => 'events',
'singular' => 'Event',
'plural' => 'Events',
'supports' => array('title', 'editor', 'custom-fields', 'page-attributes','excerpt','thumbnail'),
'icon' => 'dashicons-calendar',
),
);
foreach ($arr_custom_post_type_options as $cpt_opts) {
$label = $cpt_opts['label'];
$labels = array(
'name' => $cpt_opts['plural'],
'singular_name' => $cpt_opts['singular'],
'menu_name' => $cpt_opts['plural'],
'parent_item_colon' => 'Parent:',
'all_items' => $cpt_opts['plural'],
'view_item' => 'View',
'add_new_item' => 'Add New',
'add_new' => 'Add New',
'edit_item' => 'Edit',
'update_item' => 'Update',
'search_items' => 'Search ' . $cpt_opts['plural'],
'not_found' => 'None found',
'not_found_in_trash' => 'None found in Trash',
);
$args = array(
'label' => $label,
'description' => $cpt_opts['plural'],
'labels' => $labels,
'supports' => $cpt_opts['supports'],
'hierarchical' => true,
'public' => false,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => false,
'show_in_admin_bar' => true,
'menu_position' => 25.3,
'menu_icon' => $cpt_opts['icon'],
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'rewrite' => true,
'capability_type' => 'post',
);
register_post_type($label, $args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment