custom template for custom post type
Home » BLOG » WordPress » How to create custom templates for custom post type in WordPress

How to create custom templates for custom post type in WordPress

category:  WordPress
[Sassy_Social_Share] 4 min read

“How to create custom templates for custom post type in WordPress”, is one of the most popular questions from the internet.

Before we build the custom template for the custom post type, you should understand how WordPress decides which template file will be used on individual page.

Template hierarchy

Template hierarchy is the logic WordPress uses to decide which template file will be used for each individual page. Here is a interactive template hierarchy.

diagram shows which template file is called to generate a WordPress page based on the WordPress template hierarchy
Figure: Diagram shows which template file is called to generate a WordPress page based on the WordPress template hierarchy

Note that, template hierarchy will help you know which template file needs to be edited if you want to customize your theme.

Query string

WordPress uses the query string to decide which template file should be used to display on the screen.

Here is an example of query string. Start with question mark then the parameter and the value. Each parameter is separated by ampersands(&).

http://yourdomain.com/?cat=13&limit=10

Most of the time, your site will set the URL as pretty link which is a SEO URL friendly.

Short explanation

Please open the diagram from this link and read this explanation along.

When the individual page is requested from the browser (meaning the user clicks on your WordPress link), WordPress will look for the template file with specific name in the current theme’s directory. Then use the first matching template file following the template hierarchy.

For example, I click on the dogs category link which will display all posts from the dogs category. The link is “https://yourdomain.com/blog/category/dogs

  • First, WordPress will look for the template file in the current theme’s directory that matches the category’s slug. From our example link, WordPress looks for category-dogs.php.
  • If category-dogs.php is not found in the current theme’s directory, WordPress will look for the template file that matches the category’s ID. From our example link, WordPress looks for category-1.php since my dogs category ID is 1.
  • If category-1.php is not found in the current theme’s directory, WordPress will look for a generic category template file which is a category.php.
  • If category.php is not found in the current theme’s directory, WordPress will look for a generic archive template file which is a archive.php.
  • If archive.php is not found in the current theme’s directory, WordPress will look for a main theme template file which is index.php. Index.php file is a MUST template file that each theme needs.

As you can see WordPress hierarchy is very useful when you are working on the custom template for your WordPress site. To create the custom post type template is simple as the example above.

Custom Post Type templates

Create new template

Let’s say, we create a project post type as our custom post type. I want to create two more templates for displaying the posts from the project posts. I want to create a single post template for displaying the content from one project and I want to create an archive template for displaying all projects.

From the template hierarchy diagram, I need to create the single-{$post-type}.php and the archive-{$post_type}.php while $post_type is a $post_type argument in the register_post_type function. Register_post_type function is a function to register the custom post type.

That means I need to create:

  • single-project.php
  • archive-project.php

If you want to have your custom post type templates look the same as your theme (header, sidebar, footer, look and feel), you should copy the code from the single.php and archive.php from your current theme’s directory and modify the query to fit your needs in your custom post type templates.

Use the generic WordPress template

However, if you don’t want to create the custom post type templates, WordPress will use the generic WordPress template files instead. In our case, WordPress will look for the single.php and archive.php to display our project posts. If no archive.php in the current theme’s directory, index.php will be used following the template hierarchy.

And that how you create the custom post type template for your custom post type.

Create page template

Apart from the custom post type template, you can create a page template that apply only to pages to change their look and feel. The page template can be applies to the single page, a page section or a class of pages. Follow this link to learn more from WordPress theme handbook.

Reference

  • Template File Hierarchy : How WordPress determines which template file will be used on the individual page.
  • Custom Post Type Template Files : An article from WordPress Handbook.
  • Template Files : What is the template files in WordPress.
  • Conditional Tags : Condition tags is used in the WordPress template files for displaying the content depending on the conditions. For example, displaying the welcome message for the logged in users and hiding the welcome message for the guest users. Or showing the edit page link for the logged in users while they are visiting on home page only.
  • Template Tags : It is used within the theme to retrieve content from your database. You should use Template Tags rather than doing your own query. Of cause, if no Template Tags serves your needs, you still do your own query by WP_QUERY. Here is some of Template tags you may see in your theme.
  • Theme Security : As a WordPress developer, you must develop the WordPress theme and plugins with a security mindset always. Pick the secure server and secure mail server for your clients. Let your clients know why security is very important.