Skip to Main Content

How to create custom templates on WordPress

Divi: the easiest WordPress theme to use

Divi: The best WordPress theme of all time!

With over 901.000 downloads, Divi is the most popular WordPress theme in the world. It is complete, easy to use and comes with more than 62 free templates. [Recommended]

Do you want to create a unique custom template on WordPress?

Custom templates allow you to easily select different templates for your publications.

In this tutorial, we will show you how to easily create unique custom templates on WordPress.

But before, if you have never installed WordPress discover How to Install a WordPress Blog in 7 Steps et How to search, install and activate a WordPress theme on your blog 

Then let's come back to what we are here for ...

Note: This tutorial requires you to edit the WordPress theme files. If you haven't already, check out our tutorial on how to create a WordPress plugin.

When do you need a unique custom post template?

Sometimes you may want a different look for some publications on your website. For example, you may want to use a different template for articles or articles in a particular category.

See also How to create custom templates on WordPress

This is where you will need to create a unique custom WordPress post template.

How to create a custom publishing template?

Create a new article, then on the “Page Attributes” metabox on the sidebar of your edit area. Then select your model from the list of models there. And that's all !

Next, let's look at how to easily create unique custom templates on WordPress.

Creating unique custom templates in WordPress

First you need to open a text editor on your computer like Notepad and paste the following code inside:

<?php

/*

 * Template Name: Featured Article

 * Template Post Type: post, page, product

 */

 

 get_header (); ?>

 

This code defines a new model called " Featured Article And makes it available for article, page, and custom publishing formats.

You can save this file as " BPC-single-post.php On your desk.

Then you need to upload it to your current WordPress theme folder using an FTP client.

Then you can log into your WordPress dashboard and create or edit a post. Scroll down a bit on the post edit screen, and you'll notice the new "Page Attributes" meta-box with an option to select the template.

You will see your custom template appearFeatured Article».

Right now your template is mostly blank, if you select it you will see a blank page.

Let's correct that.

The easiest way to do this is to copy the code from your WordPress theme's single.php file and use that as a starting point.

See also How to understand the Post Type Template of WordPress

Open the file single.php and copy everything after the function get_header () ».

Easily create your website with Elementor

Elementor allows you to easily create any website design with a professional look. Stop paying expensive for what you can do yourself. [Free]

Paste this code at the end of your file BPC-single-post.php ". Now you can save this file and transfer it back to your server.

However, this looks exactly like the one of your current single publication model. You can now start making changes to your unique custom template.

You can add your own custom CSS classes, remove the sidebars, create a full width model or whatever you want.

Create unique custom templates based on category

Do you want to use a custom template of unique items based on categories?

For example, articles in the "travel" category may have a different layout than those in the "photography" category.

Here's how you can do it.

First you need to add this code to the functions.php file your theme or a specific WordPress plugin for your website.

/*

* Set a constant to access our file

*/

define (SINGLE_PATH, TEMPLATEPATH. '/ single');

 

/ **

* Filter the single_template with our custom function

*/

add_filter ('single_template', 'my_single_template');

 

/ **

* Function for our template

*/

function my_single_template ($ single) {

global $ wp_query, $ post;

 

/ **

* Checks for single template by category

* Check by category slug and ID

*/

foreach ((array) get_the_category () as $ cat):

 

if (file_exists (SINGLE_PATH. '/ single-cat-'. $ cat-> slug. '.php'))

return SINGLE_PATH. '/ single-cat-'. $ cat-> slug. '.php';

 

elseif (file_exists (SINGLE_PATH. '/ single-cat-'. $ cat-> term_id. '.php'))

return SINGLE_PATH. '/ single-cat-'. $ cat-> term_id. '.php';

 

endforeach;

}

This code first checks if WordPress is requesting a single post. If so, it tells WordPress to look for the template in your WordPress theme's “/ single /” folder.

Now you need to add template files defined by this code. 

Log into your WordPress website using an FTP client or File Manager in the cPanel and go to " / Wp-content / themes / your-theme / ».

See also How to use FTP on Windows 10

Inside your current theme folder, you need to create a new folder called "unique".

Now you need to open this folder and create a new file inside. Go ahead and name this file " single-cat- {category-slug} ". Replace {Category-slug} by the category slug.

For example, if you have a category called 'News', you will create a file " single-category-news.php ". If you have a category called "Travel Tips»Create a template single-cat-tips-of-voyage.php, And so on.

Now, these template files will be totally empty. As a starting point, you can copy the contents of your file single.php from your theme folder and paste them into each of these templates.

You can now edit these templates to make the desired changes.

Once you are done, you can access your website and view a publication. It will use the template you created for the category where this publication is attached.

Read also our tutorial How to manage WordPress files and folders

Now, suppose you have a post posted in two categories of news and travel advice. WordPress will automatically display the template for 'News'because it appears first in alphabetical order.

On the other hand, if you have dropped a post in a category and you have not created a template for that category, WordPress will revert to the template. single.php default of your WordPress theme.

Discover also some premium WordPress plugins  

You can use other WordPress plugins to give a modern look and optimize the grip of your blog or website.

We offer you here some premium WordPress plugins that will help you do that.

1. Social Wall Addon for UserPro

UserPro can be a lot more fun with a social wall. This is what “Social Wall Addon for UserPro” brings. With the Social Wall plugin, you can add a wall to your blog or website and provide your members with a refreshing way to interact with the entire community.

The WordPress Social Wall plugin allows your users to: share messages with each other, share images, comment on other people's posts, users can like or dislike a message, they can choose to display the wall only for them. logged in users or for all users, administrators can delete user posts or comments, etc.

Download | Demo | Web hosting

2. WooCommerce Advanced Categories

Thanks to this powerful WordPress plugin, put your online store on the same level as the largest stores in the world. It allows you to improve your SEO by adding texts linked to a category. You can also add a nice header image to your categories and give your subcategories more style.

You will have total control over your WooCommerce categories and their layout with this WordPress plugin. In particular, you can display the subcategories you want and also modify or arrange them in columns. You will be able to define the style, the font and even give them a short description.

Download | Demo | Web hosting

3. WavePlayer

As you can guess from the name it bears, WavePlayer is an audio player plugin that takes the waveform of the playing audio file. With this plugin, you will be able to host tracks or simply integrate it with other cloud hosting services, such as SoundCloud.

It can also be used for podcasting purposes and it offers other features such as: HTML5 support, responsive layout, WooCommerce integration, etc. You will also be able to visually create a playlist before publishing it. Another essential feature is its speed and efficiency.

Download | Demo | Web hosting

Other recommended resources

We also invite you to consult the resources below to go further in the grip and control of your website and blog.

 

 

Conclusion

Here ! That's it for this tutorial, I hope it allows you to create custom post templates on WordPress. feel free to share the tip with your friends on your social networks.

However, you will also be able to consult our resources, if you need more elements to carry out your projects of creation of Internet sites, by consulting our guide on the WordPress blog creation.

But in the meantime, tell us about your Comments and suggestions in the dedicated section.

... 

 

This article features 0 comments

Leave a comment

Your email address will not be published. Required fields are marked with *

This site uses Akismet to reduce unwanted. Learn more about how your comments data is used.

Back To Top
7 shares
Share5
tweet1
Save1