A Custom post type is a powerful way to extend the functionality of your WordPress site. Instead of being limited to the default Pages and Posts, you are able to add and customize any kind of post type you would like. You can do this by adding some simple code to the functions.php file of your WordPress theme.
Quick and Simple Way to Add a Custom Post Type
For example, let’s say you want to feature cars on your website and you want to setup a custom post type for this. All you need to do is add in this code to your functions.php file to make it work:
add_action('init', 'create_post_type');
function create_post_type() {
register_post_type('cars',
array(
'labels' => array(
'name' => __('Cars'),
'singular_name' => __('Cars')
),
'public' => true,
'has_archive' => true,
)
);
}
That’s it! You’re now ready to use your new custom post type. If you return to your WordPress Dashboard, you’ll notice that “Cars” now appears in your admin menu.
A Bit More Functionality
While the above code will get the job done, there are plenty of times where you may need to customize and extend the functionality of your post type a bit more. For example, you may want your new “Cars” post type to be organized by categories or tags. In order to accomplish this, you’ll need to write a bit more code, but it’s still pretty straightforward.
In order to have all the options for a post type open to you, drop this code into your functions.php file:
add_action('init', 'cars', 0);
function cars() {
$labels = array(
'name' => _x('Cars', 'Post Type General Name', 'theme'),
'singular_name' => _x('Car', 'Post Type Singular Name', 'theme'),
'menu_name' => __('Cars', 'theme'),
'name_admin_bar' => __('Cars', 'theme'),
'parent_item_colon' => __('Parent Car:', 'theme'),
'all_items' => __('All Cars', 'theme'),
'add_new_item' => __('Add New Car', 'theme'),
'add_new' => __('Add New', 'theme'),
'new_item' => __('New Car', 'theme'),
'edit_item' => __('Edit Car', 'theme'),
'update_item' => __('Update Car', 'theme'),
'view_item' => __('View Cars', 'theme'),
'search_items' => __('Search Cars', 'theme'),
'not_found' => __('Not found', 'theme'),
'not_found_in_trash' => __('Not found in Trash', 'theme'),
);
$args = array(
'label' => __('cars', 'theme'),
'description' => __('Cars', 'theme'),
'labels' => $labels,
'supports' => array('title', 'thumbnail', 'editor'),
'taxonomies' => array('category', 'post_tag'),
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'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',
'menu_icon' => 'dashicons-admin-post',
);
register_post_type( 'cars', $args );
}
That may seem like a lot of code, but most of these are straightforward and simply tell WordPress how to display certain things on the admin side. You’ll notice that the categories and tags for your post type have been enabled on this line ‘taxonomies’ => array(‘category’, ‘post_tag’).
Some other handy things to know are, for example, if you want to have an archive of your posts, you need to be certain that the archive option is set to “true” like this: ‘has_archive’ => true. To add or remove certain editing capabilities such as the text editor or post title, you can manipulate the ‘supports’ parameters. Lastly, if you want your custom post type to have a unique menu icon, you can change this by altering this line: ‘menu_icon’ => ‘dashicons-admin-post’. A list of icons you can use are available here.