Wordpress Custom Post Template

admin 11/22/2021
82 Comments

Topics

  • Author.php and Date.php

Page templates use Template Name whereas post templates use Template Name Posts so you’ll need to change this info at the top of each new post template. To change this template name simple click Code Edit in File Manager and change Template Name to Template Name Posts. You’ll now see your new custom templates in the drop down. To use this template, edit the desired post from the WordPress admin and assign the template from the dropdown menu. If you check the post on the front end, it’s still not full-width, but the title and meta data have swapped places, so we know that the post is using our custom template We have to add some CSS to make it full width.

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.

Index.php Index.php

index.php will display Post post types if there is no other template file in place. As stated in many places, every theme must have an index.php file to be valid. Many basic themes can get away with just using the index.php to display their Post post types, but the use cases given above would justify creating other template files.

Often you will want unique content structure or layout depending on what is being displayed. There are many templates you can use to customize content structure based on the context within the site. The two most notable post template files are home.php and single.php which display a feed of posts and a single post respectively.

Home.php Home.php

When a static front page is used and the site has a page defined for the blog list the home.php file is used for the designated blog list page. Use of this template is encouraged over creating a custom page template because blog pagination on a custom page template will not work properly. If there is no home.php in the theme index.php will be used instead.

Single.php Single.php

It’s good sense to build as simply as possible in your template structure and not make more templates unless you have real need for them. Therefore, most theme developers don’t create a single-post.php file because single.php is specific enough. For the most part, all themes should have a single.php. Below is an example of a single.php file from the theme Twenty Fifteen.

Website

In the code example above you can see the header is pulled in with get_header() then there are a two html tags. Next the Loop starts and the template tagget_template_part( 'content', get_post_format()); pulls in the appropriate content by determining the post type with get_post_format(). Next, comments are pulled in with the template tag comments_template(). Then there is some pagination. Lastly, the content divs are closed and then footer is pulled in with get_footer().

Singular.php Singular.php

WordPress Version 4.3 added singular.php that comes in the hierarchy after single.php for posts, page.php for pages, and the variations of each. This template follows the rules of is_singular() and is used for a single post, regardless of post type. Themes that used the same code for both of those files (or included one in the other) can now simplify down to the one template.

Archive.php Archive.php

Unless a developer includes meta data with permalinks in their templates, the archive.php will not be used. Meta data is information tied to the post. For example the date something was posted on, the author, and any categories, tags, or taxonomies used for the post are all examples of meta data. When a visitor to a website clicks on the meta data, the archive.php will render any posts associated with that piece of meta data. For example, if a visitor clicks on the name of an author, the archive.php will display all posts by that author.

Commonly, the title of the page being displayed by archive.php will be the name of the meta data the user clicked on. So if the user clicked on the Author’s name, the page name displaying all the other author’s posts will be the Author’s name and frequently there might be an additional description about the meta data. Here is a code example from Twenty Fifteen on their achive.php file. This snippet is the only piece of code that makes the archive.php file different from a home.php or index.php file.

Author.php and Date.php Author.php and Date.php

Author.php and date.php are more specific archive type files. If you need a refresher check out where they fit within the template heirarchy. Generally, archive.php will suffice for most themes’ needs and you won’t need to create these templates.

Author.php Author.php

If you are building a theme designed for multiple authors, it might make sense to build an author.php template. In the author.php template you could provide more information about an author, their gravatar, pull in their social media sites, and then all posts written by them. This would be a step up from relying just on the archive.php file.

Additionally, you can build specific author.php files for individual author’s by using their author ID or nicename. For example, say John Doe is the head author for a site with many guest authors. You may want all the guest authors’ information to display with author.php but you might build a specific author page with more information for John Doe by creating author-johndoe.php or author-3.php if his author ID is 3.

Date.php Date.php

Similarly, if you are building a theme directed at magazine or news websites, a date.php file might make sense to build as these websites frequently organize their articles and posts by date or issue. Additionally, you could build a day.php, month.php, or year.php if you found enough justification for it.

Category.php, Tag.php, and Taxonomy.php Category.php, Tag.php, and Taxonomy.php

If you need a refresher on what categories, tags, & taxonomies are you can look at their page. Often you won’t need to build out these template files. However, in an example of building a theme for food bloggers, there are some use cases for building these specific templates. In a food blogger website, the categories could be Great Restaurants, Beautiful Food, Ethnic Cuisine, and Recipes.

You might want most of your blog posts to display the same way except for any blogs that are categorized as recipes, because all recipes have ingredients and instrucitons sections. Therefore, you may want to build a category-recipe.php file to display your recipe blog posts in a grid view with some of the important details about the recipe visible.

Additionally, perhaps chocolate is a really important tag for the theme you’re building. It might make sense to build a tag-chocolate.php file so that you can display a specialized banner image of chocolate.

Search.php Search.php

Most themes have a search.php file so it is clear to users that their query went through. It is common to have some sort of header identifying the query results such as this snippet found int twenty fifteen’s theme.

This code snippet pulls in the query that was searched with get_search_query(). Often search.php will only pull in the excerpt instead of the full content since the user is trying to determine if the article or page fits their search.

Wordpress Custom Template Post Category

Have you ever wanted to use a different template on a post? By default, most WordPress themes don’t normally offer a range of templates to choose from to use on different posts.

Pages however, are a different story and even the free Twenty Eleven default theme for WordPress offers 7 different ones to choose from.

Video On Creating Custom Post Templates

Using the default post template on all your blog posts is a bit boring so if you want to show you subscribers a different format for a particular reason then those templates can be copied over and used to create unique posts.

Depending on the theme you have installed, yours should also offer a range of different page templates which can be used on posts as well if you follow this little trick i have for you.

Caution: Its best practice to always create a full backup of your files & databases before making changes to your WordPress site.

Tools You Can Use

For this tutorial i’ll be using the page templates from the Canvas theme and File Manager in cPanel.

You could use any theme which offers a range of different page templates and also use FTP rather then File Manager if you wish.

I’ll also be using a child theme which you can create quickly and easily in cPanel using File Manager. This way your custom post templates will not be overwritten and lost when you update your theme.

Using a Plugin

Rather than playing around with php code, the easiest way to use your page templates on posts is to simply install the custom post template plugin.

Once you’ve installed the plugin by Simon Wheatley, you’ll find a module under your post tags in your Edit Post screen. Its now time to create a post template by copying one of your existing page templates to your child themes folder.

Copy Page Templates

Using FTP or better still, File Manager in cPanel, copy the page templates you want to use for posts over to your child themes folder.

Navigate to wp-content/themes and inside you themes directory you’ll find your page templates.

Using File Manager, right click the ones you want to use as posts and copy them to your child themes directory

Note: Page templates use different names than post templates so you’ll also need to edit the name at the top of each new post template file.

Edit File Names

Page templates use Template Name whereas post templates use Template Name Posts so you’ll need to change this info at the top of each new post template.

To change this template name simple click Code Edit in File Manager and change Template Name to Template Name Posts

Change to

You’ll now see your new custom templates in the drop down.

Click the drop down to choose a new post template.

You can see from this screen shot that i’ve added 3 new templates.

Wordpress Custom Post Template Based On Category

Note: You need to copy your themes index.php file into your child themes directory as well otherwise the post templates will not work. Be careful not to move the files but copy them to your child theme.