Skip to content

Instantly share code, notes, and snippets.

@thewebprincess
Last active December 27, 2015 21:49
Show Gist options
  • Save thewebprincess/7394924 to your computer and use it in GitHub Desktop.
Save thewebprincess/7394924 to your computer and use it in GitHub Desktop.
I find this an incredibly useful snippet for adding in a number of custom post types at once in client projects. It should be pointed out that this is fine for client projects but not inclusion in projects that my want to be translated in future, or for plugins being distributed... for the same reason, it's incompatible with translation (as far …
<?php
add_action('init', 'your_plugin_slug_custom_post_types');
/**
* Create Custom Post Types.
*
* @since 1.0.0
*
* @param array $types New Custom Post Types.
*
* @return array New address formats.
*/
function your_plugin_slug_custom_post_types() {
$types = array( // Add as many new lines to this post-types array as you need for your project.
array(
'the_type' => 'portfolio',
'single' => 'Portfolio Item',
'plural' => 'Portfolio Items',
),
array(
'the_type' => 'team',
'single' => 'Team Member',
'plural' => 'Team Members',
),
);
foreach ($types as $type) {
$the_type = $type['the_type'];
$single = $type['single'];
$plural = $type['plural'];
$labels = array(
'name' => _x($plural, 'post type general name'),
'singular_name' => _x($single, 'post type singular name'),
'add_new' => _x('Add New', $single),
'add_new_item' => __('Add New '. $single),
'edit_item' => __('Edit '.$single),
'new_item' => __('New '.$single),
'view_item' => __('View '.$single),
'search_items' => __('Search '.$plural),
'not_found' => __('No '.$plural.' found'),
'not_found_in_trash' => __('No '.$plural.' found in Trash'),
'parent_item_colon' => null,
);
$icon = get_stylesheet_directory_uri() . '/images/'.$plural.'.png';
$args = array(
'labels' => $labels,
'public' => true,
'has_archive' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => 5,
'supports' => array('title','editor', 'author', 'thumbnail','custom-fields','excerpt'),
'menu_icon' => $icon,
);
register_post_type( $the_type, $args );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment