Creating new custom post template in wordpress
There are many different types of content in WordPress which are known as post types and each has different post templates. However, besides these, we can register new post types and create new post templates for those post types in WordPress in quick few steps.
Template files are used throughout WordPress themes, and it will be different from theme to theme as made available by the theme developer. Normally available post templates in WordPress are :
index.hp
home.php
author.php
single.php
Archive.php
Category.php
Tag.php
Taxonomy.php
search.php
Custom Post Types
Using Custom Post Types, you can create your own post type. It is not recommended 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.
How to create a custom post template in WordPress?
To create a custom post template you may use different page builders plugins. Using page builder plugin is the most easier and common way to create a custom post template when you don’t want to bother t write code at your own. However, you can create a custom post template by writing code by yourself for every single post or page in WordPress.
Manually Creating Custom Single Post Templates in WordPress by writing code by yourself.
This method is a bit advance as it requires you to edit theme files or add a new one in the theme directory, copy-paste code, and write new HTML CSS.
First, you need to open a plain text editor on your computer like Notepad and paste the following code inside it:
<?php
/*
* Template Name: Custom Post type
* Template Post Type: post, page, product
*/
?>
Bingo You have successfully created a new post template in the WordPress. However if you select this template and publish and try to view this on a browser it will only show you a blank page. Let’s fix how to get content on this template.
The easiest way to do that is by copying the code from your theme’s single.php(originally most of the theme has the template with this codes) file and use it as a starting point.
Open the single.php file and then copy everything after the get_header();
and get_footer()
; line.
Paste this code in your single-custom-post.php
file at the end, then save the file with as before. Now your code should look like this .
<?php
/*
* Template Name: Custom Post Type
* Template Post Type: post, page, product
*/
get_header();
get_footer(): ?>
However, this will look exactly the same as your current single post template. You can now start making changes to your custom single post template.
You can add your own custom CSS classes, remove sidebars, create a full-width template, or anything you want. You can start adding your new class or codes for new custom post type template in between get_header() and get_footer() so that everything you want to make changes or display comes into the body part of your page.