I say the same shtick every time when I’m explaining why we use and recommend WordPress: it’s flexible, so you can build whatever you need, without having to overbuild anything.
Given its history as a blog platform and not a “full-featured” content management system (CMS), it’s not a surprise that WordPress generally breaks down content into two different types:
It’s straight forward and simple.
But what do you do when you have content that isn’t necessarily date-based, but needs to be categorized? Or when you have date-based content but isn’t your typical blog post or news item?
Enter Custom Post Types.
WordPress allows you to create custom post types, which are treated as their own type of content, separate and apart from your pages and posts.
Here’s the official definition of them, from WordPress.org:
Whilst there are already lots of standard post types within WordPress, you may want to extend the amount of post types you have if you want to break things down into smaller categories. For example, if you want to have a section on Books, it would be better suited to creating a custom post type for them.
So why would you want to have a specific custom post type just for, say, Books?
Let’s say your website reviews books and includes affiliate links to buy those books on Amazon. Here’s a few reasons why you’d make Books a custom post type:
You can extend this logic out to any type of content you have with specific needs that differ from traditional posts or pages. Custom post types allow you to better organize your content, without needing to jerry-rig your existing content types.
Thankfully, it’s very easy to create and manage your custom post types.
Not surprisingly, there are a number of WordPress plugins that will do it for you, including the Custom Post Type UI and Custom Post Type Maker plugins.
But as you know, we focus on minimalist code in our website development, so our preferred method is using the register_post_type
function in WordPress.
Here’s what it looks like to register two custom post types in a typical functions.php file.
/*Custom Post Types*/
add_action( 'init', 'create_cpts' );
function create_cpts() {
register_post_type( 'staff',
array(
'labels' => array(
'name' => __( 'Staff' ),
'singular_name' => __( 'Staff Member' ),
'add_new_item' => __( 'Add New Staff Member' )
),
'public' => true,
'menu_icon' => 'dashicons-businessperson',
'rewrite' => array('slug' => 'staff'),
'menu_position' => 20,
'supports' => array( 'title', 'editor', 'author','thumbnail' )
)
);
register_post_type( 'resources',
array(
'labels' => array(
'name' => __( 'Resources' ),
'singular_name' => __( 'Resource' ),
'add_new_item' => __( 'Add New Resource' )
),
'public' => true,
'menu_icon' => 'dashicons-portfolio',
'rewrite' => array('slug' => 'resource'),
'menu_position' => 20,
'supports' => array( 'title', 'editor', 'author','thumbnail','excerpt' )
)
);
}
Reviewing this code, all of your custom post types must or should have:
(The entire description of how to use register_post_type()
can be found in the WordPress Developer Resources)
Once you register your custom post type, you’ll see it appear in the backend of your WordPress site, and then you can create away!
Important: it is often the case that, after creating a custom post type, you’ll get an error when trying to view your content on the front-end for the first time. If this happens, don’t worry! Go to Settings -> Permalinks and click ‘Save’. Try again, and it’ll work!
Looking to extend your custom post types even further? Try these on for size.
If you want to organize your custom post types beyond the traditional Categories and Tags, you can create Custom Taxonomies.
Let’s say you wanted to have different ‘Types’ of Resources so you can easily filter. Here’s how you would do that.
/*Custom Post Type Taxonomy*/
add_action( 'init', 'create_taxs' );
function create_taxs() {
register_taxonomy(
'types',
'resources',
array(
'labels' => array(
'name' => __( 'Types' ),
'singular_name' => __( 'Type' ),
'add_new_item' => __( 'Add New Type' ),
'edit_item' => __( 'Edit Type' ),
'update_item' => __( 'Update Type' ),
'new_item_name' => __( 'New Type' ),
'search_items' => __( 'Search Types' ),
'not_found' => __( 'No Type found.' ),
),
'hierarchical' => true,
'rewrite' => array( 'slug' => 'type' ),
'public' => true,
'show_admin_column' => true,
)
);
}
What if you wanted to use an icon from the amazing Font Awesome library for your custom post type?
You can make that happen by loading the Font Awesome stylesheet and targeting CSS to replace the default icon. Here’s how we did that for our clients at Silver Branch Brewing, who have a custom post type for their beers.
function fontawesome_dashboard() {
wp_enqueue_style('fontawesome', 'https://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css', '', '4.0.3', 'all');
}
add_action('admin_init', 'fontawesome_dashboard');
function fontawesome_icon_dashboard() {
echo "<style type='text/css' media='screen'>
#adminmenu .menu-icon-beers div.wp-menu-image:before {
font-family: Fontawesome !important;
content: '\\f0fc';
}
</style>";
}
add_action('admin_head', 'fontawesome_icon_dashboard');
There’s plenty that you can do with Custom Post Types, and I highly encourage you to use them. It makes organizing your content so much easier, and you’ll never feel like you’re bending over backwards to get something to work the way it should.
Have a question about how your content is organized? Reach out to us and we’ll be happy to help.