I recently came across an issue on a client project, and that was: the requirement to create multiple templates for a CPT or custom post type. This client runs events, and each event should have a specific CPT with it’s own branding, style guide etc.
But first: What is a custom post type?
Custom post types are specific, custom code-added posts that allow you to keep your site flexible. For example: You might want a section for members of the team and add them in different ways on their own page or list inside of WordPress itself with no need go through complicated editing processes every time someone joins/leaves- just edit one place!
Custom post types are a great way for marketers and site managers alike to add new functionality without having to edit template files, which should strictly be done by a WordPress developer.
First, let’s define what Custom Post Types are, how taxonomies are used, and how they integrate with ACF:
Custom Post types allow you to create a new admin interface for managing content, media, functionality, and you can also create a custom filter or use a plugin to allow you to search by taxonomies. The main benefit a business gets by having custom post types is using different page templates, for each CPT. For example a book store may have custom post types, sorted by category of fiction, non-fiction, children’s and cooking. This means the user of the site can search and filter by these categories. It also means a custom template file can be created for each category- meaning you have a base design and configuration for each category without having to design a page from scratch.
The categories are a great way to organise your posts, depending on what you have activated in the theme. Categories can be used for organising your menu into category pages and displaying them by listing all of their content by for example a category of cooking. Once you have multiple categories setup as CPT, you can click on the category eg: cooking, and see every post with that category without having to trawl through the website or use WordPress default search function by keyword.
You can also add tags to your content which are searchable, as you normally would with WordPress posts. Categories and tags are known as taxonomies in WordPress.
Advanced custom fields
Custom fields are a great way to add additional information you want that can be used later in your theme. In our book store example, custom fields allow you to add more information about your book post such as its author or qualifications, which could also add to your search and filter function.
Now that we have cleared that up, let’s go back to the headline of this article which is how to have multiple custom post type templates.
How to use multiple custom post type templates in WordPress
When creating a new CPT, WordPress will search for the CPT template file that it will use for each category, and this is declared in your template files like so:
For this example, I am using a custom post type category of events. WordPress will search for a template file with the template post type of ‘event’, and use that template to display your event page.
When you have only one template file, your CPT backend looks like this:
You can see on the right hand side there is no option to select a different template file, like you usually have when you are creating a new page. This is because we only have one template file in use for the CPT with category of event.
So, we need to create another template file for the category of event, and we will have two template files to choose from.
My reason for doing this is the current event template is built in CSS Grid, which is not user friendly for the marketing manager who wants to use it and doesn’t know CSS. She wants a standard WordPress template, so I have copied the content of my page.php file from my parent theme, and into a new template file called vendor-event.php, which means we have a second event template for her to use with the usual WordPress page layout. Here is the code I used:
After you have created your second CPT template file, you can go to your CPT category in your dashboard – in this case event, click on ‘add new’ and now you can see there are two template files to choose from before you build your new page- one in CSS Grid and one in Flexbox.
Alpha Omega Digital is a WordPress agency based in Melbourne, Australia but also services clients from Sydney, Brisbane, Newcastle, Perth, Adelaide, Darwin and Hobart. Have a project in mind? Contact me here.