Wordpress Custom Post Type Template

Posted onby admin

Topics

The easiest way to create a custom single post template is by using Beaver Builder. It is the best WordPress page builder plugin on the market and allows you to create a post template without writing any code or modify your WordPress theme. The first thing you need to do is install and activate the Beaver Builder plugin. 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 acmeproduct custom post type, a good place to start is by copying the single.php file, saving it as single-acmeproduct.php and editing that.

  • 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
Type

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.

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.

Read more about the The Template File Hierarchy: https://developer.wordpress.org/themes/basics/template-hierarchy/

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

Prerequisites

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.

Examples:

  • 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.

Select your WordPress Front page

Wordpress Custom Post Type Page Template

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.

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.

Read more athttps://wordpress.org/support/article/creating-a-static-front-page/

Page Templates

Wordpress Custom Post Type Template File

Custom post type template

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-{slug}.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-{id}.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.

Read more at https://developer.wordpress.org/themes/template-files-section/page-template-files/

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).

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.

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.

Read more: https://developer.wordpress.org/themes/template-files-section/post-template-files/

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.

Wordpress Add Custom Post Type

The two most notable post template files are:

  • single-{post-type}.php This single post template is used when a visitor requests a single post from the corresponding custom post type.
  • archive-{post-type}.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.

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.

Read more: https://codex.wordpress.org/Function_Reference/post_class