Wordpress Custom Post Type Template

admin 11/23/2021
  1. Wordpress Custom Post Type Page Template
  2. Wordpress Custom Post Type Template In Plugin
  3. Wordpress Custom Post Type Template Example
  • If your custom post type were 'product', and/or queryvar = 'product', WordPress would look for single-product.php to display the single or permalink of the post. If these files are not available in your Theme's directory WordPress will look for archive.php and single.php, respectively.
  • Template page attributes for custom post type is one of them. We have two custom post types registered, “Code Snippet” and “Case Studies.” I wanted to use the existing theme template for these post types. But, Template from Page Attributes available for only ‘page’ post type. So, here are what we did to.

WordPress custom post type has its own menu in the WordPress admin area, which leads to a list of posts of that particular type. In the example above, if a user goes to the Custom Articles database section of your site, the posts from Post, or Page will not be included. That’s because they belong to their own types. I'd like to offer my custom post type as a Plugin, so that people can use it without touching their theme folder. But custom post type templates - such as single-movies.php - reside in the theme.


  • Default Post Types

There are many different types of content in WordPress. These content types are normally described as Post Types, which may be a little confusing since it refers to all different types of content in WordPress. For example, a post is a specific Post Type, and so is a page.

Internally, all of the Post Types are stored in the same place — in the wp_posts database table — but are differentiated by a database column called post_type.

In addition to the default Post Types, you can also create Custom Post Types.

The Template files page briefly mentioned that different Post Types are displayed by different Template files. As the whole purpose of a Template file is to display content a certain way, the Post Types purpose is to categorize what type of content you are dealing with. Generally speaking, certain Post Types are tied to certain template files.

Default Post Types Default Post Types

There are five default Post Types readily available to users or internally used by the WordPress installation:

  • Post (Post Type: ‘post’)
  • Page (Post Type: ‘page’)
  • Attachment (Post Type: ‘attachment’)
  • Revision (Post Type: ‘revision’)
  • Navigation menu (Post Type: ‘nav_menu_item’)

The Post Types above can be modified and removed by a plugin or theme, but it’s not recommended that you remove built-in functionality for a widely-distributed theme or plugin.

The most common post types you will interact with as a Theme Developer are Post, Page, Attachment, and Custom Post Types. It’s out of the scope of this handbook to flesh out the Revision and Navigation Menu Post Types. However, it is important to note that you will interact with and build the functionality of navigation menus and that will be detailed later in this handbook.

Post Post

Posts are used in blogs. They are:

  • displayed in reverse sequential order by time, with the newest post first
  • have a date and time stamp
  • may have the default taxonomies of categories and tags applied
  • are used for creating feeds

The template files that display the Post post type are:

  • single.php and single-post.php
  • category.php and all its iterations
  • tag.php and all its iterations
  • taxonomy.php and all its iterations
  • archive.php and all its iterations
  • author.php and all its iterations
  • date.php and all its iterations
  • search.php
  • home.php
  • index.php

Additionally, theme developers can display Post post types in front-page.php if they so desire.

Read more about Post Template Files.

Page Page

Pages are a static Post Type, outside of the normal blog stream/feed. Their features are:

  • non-time dependent and without a time stamp
  • are not organized using the categories and/or tags taxonomies
  • can have page templates applied to them
  • can be organized in a hierarchical structure — i.e. pages can be parents/children of other pages

The template files that display the Page post type are:

  • page.php and all its iterations
  • $custom.php and all its iterations
  • front-page.php
  • search.php
  • index.php

Read more about Page Template Files.

Attachment Attachment

Attachments are commonly used to display images or media in content, and may also be used to link to relevant files. Their features are:

  • contain information (such as name or description) about files uploaded through the media upload system
  • for images, this includes metadata information stored in the wp_postmeta table (including size, thumbnails, location, etc)

The template files that display the Attachment post type are:

  • MIME_type.php
  • attachment.php
  • single-attachment.php
  • single.php
  • index.php

Read more about Attachment Template Files.

Custom Post Types Custom Post Types

Using Custom Post Types, you can create your own post type. It is not recommend that you place this functionality in your theme. This type of functionality should be placed/created in a plugin. This ensures the portability of your user’s content, and that if the theme is changed the content stored in the Custom Post Types won’t disappear.

You can learn more about creating custom post types in the WordPress Plugin Developer Handbook.

While you generally won’t develop Custom Post Types in your theme, you may want to code ways to display Custom Post Types that were created by a plugin. The following templates can display Custom post types:

  • single-{post-type}.php
  • archive-{post-type}.php
  • search.php
  • index.php

Additionally, Theme Developers can display Custom Post Types in any template file, often by using multiple loops.

Read more about Custom Post Type Templates.

We need to create few templates to display custom post types content in WordPress. In the previous article, we have created few posts in our Books post types with some custom content fields. Here I will update you how to display those custom content using separate templates in WordPress for the custom post type.

Here you will know WordPress templates hierarchy as well as how to display custom content there. We have created those content using Advanced Custom Fields plugin. This is the 5th article of our series on Custom Post Types (CPT). The topics before and after this article are:

Create templates to display custom post types content

Exporting custom taxonomies/fields & further reading

WordPress Templates Hierarchy and CPT

First, you must know what kind of templates are required to show custom content. if we create our CPT with the parameter has_archive set to either true or a string, then it will display all books in an archive page. The WP hierarchy is an order to pick a matching template here.

For the custom post type, it will look for:

archive-{post_type}.php and

If these templates are not present in the theme, then it will look for the general templates:

archive.php and

Wordpress Custom Post Type Page Template

Even if these templates are not defined then the index.php is the default one to use. So, templates for the book post type will be archive-book.php and single-book.php.

Two Notes to Display Custom Post Types Content

The list of books will be displayed on the book archive page, and that will be located on this page:


If you have the pretty permalinks enabled, then you can also access the page with this URL:


Wordpress Custom Post Type Template

The first thing to note is that the pretty URL above has the plural form of the post type. We used a singular word (book) in the register_post_type() function which is the identifier for the post type. Further, the has_archive parameter allows us to customize the slug of the archive page by passing a string to it.

We have passed books there so the archive page URL is different and in plural form. Instead, if you pass true there then it will be the same as the slug key in rewrite parameter. It’s your choice to select the name and slug of CPT, just keep this concept in the mind. In addition, you’re free to use a plural word for the post type. Moreover, WordPress itself uses single words for them.

Second, if you’re using taxonomies and terms in your custom post types then different templates might require. According to WordPress documentation on templates hierarchy, the order is:


So if you have Genre (genre) taxonomy and Fiction (fiction) term in book post type, then you can create the following templates as per requirements:


Templates to Display Custom Post Types Content

The best way to create a new template file is copy code from existing similar template in the theme. Then paste in a new PHP file and save it as the template name discussed above. Finally, modify the code carefully and save changes.

So you have to create the single-book.php template to display single post of the book post type. Similarly, you can create archive-book.php to show the list of book posts. Also, if you require modifications in taxonomies or terms related templates, use the specific template as already described.

I’m here updating you with functions to display ISBN, Price and Publisher custom fields in templates. As we have created those fields using Advanced Custom Fields plugin previously here.

Display Custom Post Types Content - ACF

Wordpress Custom Post Type Template In Plugin


So the above is the explanation of how to display custom post types content using templates in WordPress. It’s all about custom post types and custom content or fields. The last article focuses further reading and recommendations, renaming CPT and backup topic.