Custom post type template wordpress

Developer Resources


Custom Post Type Template Files


The WordPress theme system supports custom templates for custom post types. Custom templates for the single display of posts belonging to custom post types have been supported since WordPress Version 3.0 and the support for custom templates for archive displays was added in Version 3.1.

Custom Post Type – Template Hierarchy

WordPress will work through the template hierarchy and use the template file it comes across first. So if you want to create a custom template for your acme_product custom post type, a good place to start is by copying the single.php file, saving it as single-acme_product.php and editing that.

However if you don’t want to create custom template files, WordPress will use the files already present in your theme, which would be archive.php and single.php and index.php files.

Single posts and their archives can be displayed using the single.php and archive.php template files respectively,

  • single posts of a custom post type will use single-.php
  • and their archives will use archive-.php
  • and if you don’t have this post type archive page you can pass BLOG_URL?post_type=

So for the above example, you could create single-acme_product.php and archive-acme_product.php template files for single product posts and their archives.

Alternatively, you can use the is_post_type_archive() function in any template file to check if the query shows an archive page of a given post types(s), and the post_type_archive_title() to display the post type title.


A guide to WordPress templates for posts, pages and custom post types

A website usually contains more than an index page and you probably want to create different layouts for different pages and posts for your WordPress theme. The good news is that WordPress can handle all your needs thanks to the use of specific templates that you can create with Pinegrow.

Note: This document is an updated version of our previous documentation about Pages, Posts and Custom Post types templates.

If you are new to WordPress development with Pinegrow WP or Pinegrow Theme Converter for WordPress, by following our tutorials, you have discovered how easy it is to create a basic theme by just creating an index.html page (which will be exported as index.php in the theme folder during the theme export).

Now, you want to go further and start the development of a more complex theme. For that, you need to setup WordPress behavior regarding the front page AND create specific templates based on the WordPress templates hierarchy.

WordPress themes are made up of template files. These are PHP files that contain a mixture of HTML, Template Tags, and PHP code.

To fully understand the following informations, you should be familiar with how Pinegrow WP works. Namely, the use of the WordPress > Export the theme menu item (CTRL+W on Windows or CMD+W on Mac ) shall convert your HTML files to their PHP equivalent. The name of each file remains identical, only the HTML extension is transformed into PHP.

In order to create any particular WordPress template mentioned in this documentation, you’ll have to create the corresponding file with the HTML extension in Pinegrow. It will be automatically converted to its PHP counterpart during the Theme export.

click to see the image in full size.


  • single.html will be exported as single.php
  • front-page.html will be exported as front-page.php
  • home.html will be exported as home.php
  • single-ebooks.html will be converted as single-ebooks.php
  • and so on …

OR whatever the name of your HTML template file, from the Page Settings, you can select from our list of predefined templates names.

From this list, you can cover almost 90% of the standard needs and if your need more, you will be able to manually enter the template name of your choice.

In the following screenshot, page.php is selected for export.

click to see the image in full size.

Select your WordPress Front page

By default, a WordPress site shows your most recent posts in reverse chronological order on the front page of your site BUT many WordPress users want a static front page or splash page as the front page instead.

This “static front page” look is common for users desiring static or welcoming information on the front page of the site.

The look and feel of the front page of the site is based upon the choices of the user combined with the features and options of the WordPress Theme.

click to see the image in full size.

The front-page.php template file is used to render your site’s front page, whether the front page displays the blog posts index or a static page.

The front page template takes precedence over the blog posts index template (home.php).

If the front-page.php file does not exist, WordPress will either use the home.php or page.php files depending on the setup in Settings > Reading. If neither of those files exist, it will use the index.php file.

  • front-page.php – Used for both “your latest posts” or “a static page” as set in the front page displays section of Settings >Reading in WordPress admin. (Reminder: If front-page.php exists, it will override the home.php template.)
  • home.php – If WordPress cannot find front-page.php and “your latest posts” is set in the front page displays section, it will look for home.php. Additionally, WordPress will look for this file when the posts page is set in the front page displays section.
  • page.php – When “front page” is set in the front page displays section.
  • index.php – When “your latest posts” is set in the front page displays section but home.php does not exist or when front page is set but page.php does not exist.

Page templates are a specific type of template file that can be applied to a specific page or groups of pages. These templates only apply to pages, not to any other content type (like posts and custom post types).

When a visitor browses to your website, WordPress automatically selects which template to use for rendering that page. WordPress looks for template files in the following order:

  • Page Template — If the page has a custom template assigned, WordPress looks for that file and, if found, uses it.
  • Example: page_two-columns.php (see the section about Custom Page Templates below)
  • page-.php — If no custom template has been assigned, WordPress looks for and uses a specialized template that contains the page’s slug.
  • Example: page-about.php if you want to automatically target the page with the slug name about
  • page-.php — If a specialized template that includes the page’s slug is not found, WordPress looks for and uses a specialized template named with the page’s ID.
  • Example: page-345.php if you want to automatically target the page with the ID 345

By default, entry ID’s are not visible in WordPress admin but you can install the Reveal IDs plugin to activate the display.

  • page.php — If a specialized template that includes the page’s ID is not found, WordPress looks for and uses the theme’s default page template.
  • index.php — If no specific page templates are assigned or found, WordPress defaults back to using the theme’s index file to render pages.

Custom Page and Post Templates for Global Use

Sometimes you’ll want a template that can be used globally by any page, post, or by multiple posts and pages and the process is fairly simple.

From your Pinegrow project, select (or create) a specific HTML document that you will use as your custom page or post template.

It is not mandatory, but for a better organisation or your project folder, it can be useful to name your HTML document according to its layout or use, for example two-columns.html, one-column.html

Set the Define post template action to the top node of the page from the outline tree (select the node with the page name).

click to see the image in full size.

Then, Export your theme.

That’s it, from WordPress, Edit the page or post on which you want to apply the custom template AND from the from the Page or Post Attributes section of your Page/Post, you will be able to select your Custom template from the Template dropdown menu.

click to see the image in full size.

Do not use page- as a prefix, as WordPress will interpret the file as a specialized template, meant to apply to only one page on your site.

Standard Post Templates

There are many template files that WordPress uses to display the Post post type. Any content dealing with a blog or its posts are within the Post post type. The two most notable post template files are:

  • home.php: The home page template is the front page by default. If you do not set WordPress to use a static front page, this template is used to show latest posts. If home.php does not exist, WordPress will use index.php.
  • single.php: The single post template is used when a visitor requests a single post. For this, and all other query templates, index.php is used if the query template is not present.

Custom Posts Templates

Custom post types are post types you can create according to your website needs.
You can read our introduction to Custom Post types in Pinegrow to learn more about Custom Post types.

The two most notable post template files are:

  • single-.php This single post template is used when a visitor requests a single post from the corresponding custom post type.
  • archive-.php A single page with links to all your corresponding custom post type archived entries.

You will find some VERY USEFUL details about WordPress templates by reading the WordPress Template Hierarchy

The Post class function

While this is not a template file, the following could be helpful for your developments.

WordPress theme authors who want to have finer css control options for their post styling, have the post_class function available.

When the post_class function is added to a tag within the loop, it will print out and add various post-related classes to the div tag.

It can also be used outside the loop with the optional post_id parameter. This function is typically used in the index.php, single.php, and other template files that feature hierarchical post listings.

To add the post_class function to your templates in Pinegrow, can use the WP > Posts > Post class action.

click to see the image in full size.

Important: The Post class action is ALREADY automatically set when the Show Posts smart action is set on a specific element.

Show Posts smart action automatically sets the Post Class, The id and The Loop classic WordPress actions.


Developer Resources


Working with Custom Post Types


Custom Post Type Templates

You can create custom templates for your custom post types. In the same way posts and their archives can be displayed using single.php and archive.php , you can create the templates:

  • single-.php – for single posts of a custom post type
  • archive-.php – for the archive

Where is the post type identifier, used as the $post_type argument of the register_post_type() function.

Building upon what we’ve learned previously, you could create single-wporg_product.php and archive-wporg_product.php template files for single product posts and the archive.

Alternatively, you can use the is_post_type_archive() function in any template file to check if the query shows an archive page of a given post type, and the post_type_archive_title() function to display the post type title.

Querying by Post Type

You can query posts of a specific type by passing the post_type key in the arguments array of the WP_Query class constructor.

This loops through the latest ten product posts and displays the title and content of them one by one.

Altering the Main Query

Registering a custom post type does not mean it gets added to the main query automatically.

If you want your custom post type posts to show up on standard archives or include them on your home page mixed up with other post types, use the pre_get_posts action hook.

The next example will show posts from post , page and movie post types on the home page:


Create WordPress Custom Post Type Templates

WordPress’s default post types help you create blog posts, pages, or attachment pages easily. Most of these posts and pages follow similar formats. When creating efficient content for your site, you might find the default post types restrictive. That’s where custom post types come into play.

Custom post types are content types like posts and pages generated to better suit your demand. Since they’re custom, you can call them whatever you want or add any unique design. For more specific usages, some custom post type’s layouts can differ from others. So you need to provide various templates for each custom post type, depending on your purposes.

In this post, let’s define what templates in WordPress are and how to create a custom post type template.

What Are Custom Post Type Templates?

Custom post type templates, basically a PHP file, define how your WordPress custom posts look. WordPress offers different default custom post type templates, including:

  • Single-.php – Whenever a user requests a single post from custom post type, WordPress uses the single-.php template. For example, single posts from shose_product custom post type will apply the single-shoes_product.php template.
  • Archive-.php – WordPress uses the archive template to display the custom post type archive page by default.
  • Search.php – When a visitor searches for a custom post type in your site, WordPress will use the search.php template to display search results.
  • Index.php – Once the 3 templates above are not shown up, the index.php file will be used for the custom post type.

Custom post type templates allow you to apply new layouts to your custom posts. WordPress will use the default files such as single.php, archive.php, and index.php files for your custom post types unless you create your own custom post type templates.

When Do You Need Custom Post Type Templates?

You can make multiple custom posts at the same time with a custom post type template. The following cases indicate when you should consider creating a custom post type template:

1. Coupon custom post

More than 90% of customers use coupons to make purchases. Offering discounts to your customers through blogs helps increase sales. The blog-style coupon is a custom post since it doesn’t look like any content-focused default post types. Instead, it will mainly include a short introduction of your product, and a coupon code for customers to click on and get the discount.

2. WordPress glossary

Your product involves a bunch of jargon that not all visitors or even potential customers can understand. If you don’t explain these specialized terms clearly, customers won’t make purchases. They’re not sure if your product can solve their problems. That’s where glossaries come in handy to effectively help visitors get to know your products and services.

In fact, glossaries couldn’t be created by any default post types. You need to use custom post types. Let’s take Prevent Direct Access as an example. The plugin helps protect WordPress media files for photographers, course owners, etc. Its features include a lot of technical terms such as Original links, Protected links, and more. Those who haven’t used it or referenced these terms’ definitions before will have no idea of what the plugin does. That’s why a custom post type template is created to make glossaries to support users.

3. Additional fields

You want to enter more information into your content, for example providing custom meta boxes in multiple blogs. Instead of adding these custom fields to post by post which takes a lot of time, you can properly create a new custom post type template. Any custom posts applying this template will include your desired additional fields.

How to Create a Custom Post Type Template in WordPress

There are 2 ways to create templates for custom post types. You can either edit the single.php file or use Templatic plugin.

Before diving into the 2 mentioned solutions, let’s create a child theme first. This child theme allows you to modify and save edits separately without affecting the parent theme. What’s more, you won’t lose your changes once the parent theme is updated.

We will use Twenty Nineteen, WordPress’s latest default theme to show you how to create custom post type templates. If you are using this theme, you can get started right away. In case your WordPress site still applies the previous versions, please update to this version to have full capabilities.

5 steps below show you how to generate a child theme:

  1. Go to your theme directory
  2. Create a new folder
  3. Provide a name for the theme.
  4. Add 2 new files style.css and functions.php
  5. Use the code below to set up these files

Once done, the new child theme will be shown in the Appearance section of your WordPress admin.

After generating the child theme, let’s create a custom post template.

#1 Edit single.php Files

You don’t want to insert any plugins to your WordPress site or you’ve installed many and don’t want to add any more? Then, this method is for you. Follow this 5-step-guide to easily create a custom post type template:

  1. Go to GenerateWP
  2. Provide your custom post type name in the Name (Singular) and Name (Plural) boxes
  3. Enter a descriptive summary of the post type in the Description box
  4. Hit Update Code
  5. Copy the generated code and paste in your singple.php file

#2 Create Custom Post Templates by Templatic plugin

Templatic allows you to create and choose post templates from a simple dropdown in almost the same way as adding page templates. Inserting a plugin seems more preferable than editing single.php files manually since you can avoid the complexity of code generation.

There are 2 main steps to create custom post type templates with Templatic plugin:

Step 1: Install the plugin

  1. Go to Plugin → Add New in your WordPress dashboard
  2. Search for Custom Post Templates by Templatic
  3. Click Install Now then Active the plugin

Step 2: Create custom post type templates

  1. Either duplicate your default single post template (single.php file) or create a new template file
  2. Add this code to the top of the new child file you’ve created

After creating the new custom post type template, you can click on the Post Detail Template dropdown in every post’s editing screen to select a template. If you forget to assign a template for the post, the default template will be used.

You’re probably wondering if the plugin can help design specific product pages for your WooCommerce store. Yes, it works well with WooCommerce. You’re able to create any custom templates and allocate them to any of your product pages.

Let’s Create Custom Post Type Templates for Your WordPress Site

Custom post type templates prove useful in many cases. You can apply a new layout for your posts beside WordPress default templates like single-.php or archive-.php.

There are 2 main ways to create custom post type templates, either editing the single.php file manually or installing a plugin. Before creating a custom post type template, remember to generate a child theme and make changes there instead of editing your parent theme directly so that you can reduce the risk of losing data.

If you have any concerns about creating or protecting custom post type templates in your WordPress sites, let us know in the comments section below!

Subscribe to Get our Latest Updates

Wanna get the latest WordPress news and product updates from our team? Simply enter your email below.


Читайте также:  Драйвера для принтера samsung scx 3207 series
Поделиться с друзьями