Customising WordPress – How to create custom post types in WordPress?
Contents
In my previous article on Customising WordPress, I talked about the benefits of child themes and using child themes to exercise your control over how WordPress handles your website. I had mentioned that I would show you how to add a new custom post type, new taxonomies and basic display templates for your brand shiny new custom post types.
Custom post types
What is a custom post type?
Custom post types (click to read how WordPress.org defines custom post types) are a powerful feature in WordPress that allows you to create and manage different types of content beyond the standard posts and pages. With custom post types, you can organize and display various content types in a structured manner, tailored to the specific needs of your website. Whether it’s movies, products(WooCommerce uses these as a basis of its e-commerce platform), events, portfolios, or any other unique content, custom post types provide a flexible and intuitive way to extend WordPress’s functionality. By defining custom post types, you can create dedicated sections within your website’s admin panel for managing and editing these content types separately, enabling you to present your content in a more organized and meaningful way.
Benefits of custom post type
Custom post types offer several benefits in WordPress development. They allow you to maintain a logical separation between different types of content, making it easier to manage and navigate through your website’s backend. Each custom post type can have its own set of custom fields, taxonomies, and templates, enabling you to capture and display specific information unique to that content type. Custom post types also improve the user experience by providing specialized interfaces for adding and editing content, tailored to the specific needs of each type. Additionally, custom post types enhance the scalability and extensibility of your WordPress website, allowing you to build complex and diverse websites that go beyond the limitations of standard posts and pages. With the ability to create custom post types, you have the flexibility to mold WordPress into a more robust and tailored CMS for your content management needs.
Adding a new custom post type
For our example, we are going to build a brand new custom post type call Movie(s). First of all the code:
// Register Custom Post Type
function create_movie_post_type() {
$labels = array(
'name' => 'Movies', //Name your custom post type
'singular_name' => 'Movie', //Add singular name
'menu_name' => 'Movies', //Add plural name
'name_admin_bar' => 'Movie', //What shows up in the menu bar on the left handside of the WP Admin dashboard
'add_new' => 'Add New', //Label for the add new button
'add_new_item' => 'Add New Movie',
'new_item' => 'New Movie',
'edit_item' => 'Edit Movie',
'view_item' => 'View Movie',
'all_items' => 'All Movies',
'search_items' => 'Search Movies',
'parent_item_colon' => 'Parent Movies:',
'not_found' => 'No movies found.',
'not_found_in_trash' => 'No movies found in Trash.'
);
//Define characteristics of the custom post type
$args = array(
'labels' => $labels, //Add all the labels as in the array above
'public' => true, //Make it publicly visible
'has_archive' => true, //Allow WordPress to support a dynamic page that displays all your movies
'rewrite' => array( 'slug' => 'movies' ), //URL Slug
'menu_icon' => 'dashicons-format-video', //Menu icon for WP admin dashboard
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'custom-fields' ), //Meta boxes that will show up on movie editing screen
);
register_post_type( 'movie', $args ); //Register the custom post type
}
add_action( 'init', 'create_movie_post_type' );
How to add the code to your child theme?
The block of code above is all you need to show a menu item in your WordPress dashboard called Movie. The code (in its entirety) resides in your functions.php file of the child theme. This excellent blog by JetPack has all the details you need to know about accessing and editing the functions.php file within your child theme.
Using a plugin to add the code
If for some reason, you are not using a child theme, dont worry, you can still use the code by adding a plugin called Code Snippets to add code to your WordPress functions.php. This is the most elegant way of adding the code without editing a parent theme. This plugin takes care of the code and all you have to do is to write it.
Conclusion
One of the greates strengths of WordPress as a Content Management System is the ease with which it can be customised and grown to suit our every need when it comes to displaying and managing our content. The Custom Post Type allows us to add highly customised data types into our website that meet our uniwue requirements. In this example, we have learned how to add a Custom Data or Post Type in WordPress to allow us to write about our favourite topic.
Next time
In my next article, I will show you how to create taxonomies for your Custom Post Type, Movies. Taxonomies allow website owners to group content under headings, sub-headings based on topics. These are manually created and edited so you have complete control over them. For example, Movies, we will talk about 2 taxonomies – Genre and year of Release. By the end of this series of articles, Customising WordPress, you will be able to create a custom post type, add taxonomies, create display templates and use them for promoting your content.