How to Create a Custom Page Template in WordPress

Create a Custom Page Template in WordPress

WordPress allows you to create custom layouts for all of your pages. These custom layouts are called templates.

For example, if you have a web design blog, you can create a custom template that will put the PHP logo on every page about PHP.

Some themes come with many templates already created, but creating your own is a very simple process.

In this tutorial, you will learn how to create a WordPress template that adds a PHP logo to all of your site PHP pages.


Step #1: Locate your theme’s existing pages

tutuploadsmedia_1336168606733.png
  • Using a file manager or an FTP client, locate your theme’s directory on the server.
  • Drill down to the /themes folder to see your existing page templates.
  • Locate the page.php file.
  • Open it either with a text editor or, preferably, with a code editor.

Step #2: Insert name code

tutuploadsmedia_1336173432524.png
  • Paste or type this code right before get_header(); ?>
/* Template Name: My-New-Page-Template */
  • Replace My-New-Page-Template with your own template name. You don’t need the dashes. Also, note that in the above example there’s no opening PHP tag because it’s up higher in the file. If your opening PHP tag is on the same line as the get_header(); ?> , then your code needs to go right after the opening tag (with a space between).

This code is what tells WordPress that this is a template file. We’ve used an existing page as a starter so we wouldn’t need to write all the code from scratch.

You can do that if you are creating a very custom page. Without this code, WordPress will treat this PHP file as a default template if it’s in the template’s directory. This will cause you lots of problems. Be sure to have this code at the top of any page template.


Step #3: Save your new file

tutuploadsmedia_1336170329407.png
  • Save the file with a new name and a .php extension. For example – MyNewTemplate.php.
  • Put the file on the server at /wp-content/themes/yourtheme/MyNewTemplate.php (it’s better not to have spaces in a file name).

You can name this new file almost anything you want, but there are some names that are reserved by WordPress for special purposes. I’ve included a list of those files in the notes at the end of the tutorial.


Step #4: Add a custom logo

tutuploadsmedia_1336173189966.png
  • Open the file.
  • Paste the image code and styling right before the content.
  • Add the following code to the page:
<div style="float: left; margin-right: 10px; margin-bottom: 2px;">
<img src="http://yourdomain.com/yourpix.jpg:>
</div>

Step #5: Apply your template to your pages

tutuploadsmedia_1336171527227.png
  • Go to Pages > Add new.
  • Choose My-New-Page-Template from the Page Attributes.
tutuploadsmedia_1336173131737.png

Now every page that has this template will have the PHP logo.


Reserved file names

Please do not use the following file names to name your custom page templates:

  • style.css -The main stylesheet. This must be included with your Theme, and it must contain the information header for your Theme.
  • rtl.css – The rtl stylesheet. This will be included automatically if the website’s text direction is right-to-left. This can be generated using the the RTLer plugin.
  • index.php – The main template. If your Theme provides its own templates, index.php must be present.
  • comments.php – The comments template.
  • front-page.php – The front page template, it is only used if you use a static front page.
  • home.php – The home page template, which is the front page by default. If you use a static front page this is the template for the page with the latest posts.
  • single.php – The single post template. Used when a single post is queried. For this and all other query templates, index.php is used if the query template is not present.
  • single-.php – The single post template used when a single post from a custom post type is queried. For example, single-books.php would be used for displaying single posts from the custom post type books. index.php is used if the query template for the custom post type is not present.
  • page.php – The page template. Used when an individual Page is queried.
  • category.php – The category template. Used when a category is queried.
  • tag.php – The tag template. Used when a tag is queried.
  • taxonomy.php – The term template. Used when a term in a custom taxonomy is queried.
  • author.php – The author template. Used when an author is queried.
  • date.php – The date/time template. Used when a date or time is queried. Year, month, day, hour, minute, second.
  • archive.php – The archive template. Used when a category, author, or date is queried. Note that this template will be overridden by category.php, author.php, and date.php for their respective query types.
  • search.php – The search results template. Used when a search is performed.
  • attachment.php – Attachment template. Used when viewing a single attachment.
  • image.php – Image attachment template. Used when viewing a single image attachment. If not present, attachment.php will be used.
  • 404.php – The 404 Not Found template. Used when WordPress cannot find a post or page that matches the query.

Author

0 0 votes
Article Rating
Subscribe
Notify of
guest

19 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Margaret
Margaret
12 years ago

Thank you for this article! It is just what I needed. I appreciate the list of reserved file names too.

Andre
Andre
5 years ago

Excellent.  It’s March of 2019 right now, and this article gave me EXACTLY what I wanted.
Also, I explored the possibility of creating a brand new theme for the site I am working on – but that seemed quite complicated and time consuming – especially considering you have to have support for plugins etc.
But this method let’s you do many things.  Thank-you!

Mark
Mark
5 years ago

I can’t figure out what I’m doing wrong. This seems super simple but it’s just not working for me.

Huy
Huy
5 years ago
Reply to  Mark

Just:
1. Create a new file: your-template.php
2. Open this file and add the following comment: 

3. Create a new page and now you can choose your custom template

Ahsin
Ahsin
4 years ago
Reply to  Huy

I can able to select the template that i made

Huy
Huy
5 years ago

Thank you very much! You saved my time.

Aakash
Aakash
4 years ago

wordpress so nice

Akshay Rana
Akshay Rana
4 years ago

its febraury 2020 and this arcticle give me what i really wanted thanks

John
John
4 years ago

Also, singular.php is reserved.

killerfanthose
killerfanthose
4 years ago

Thanks for this helpful Blog.

Craig
Craig
4 years ago

Something going weird for me and I never had this problem before. Except for the home page, all of my custom templates are not loading on their respective pages. It’s just loading the index page instead. I have the php file named and with the template-name showing up but it’s always the index page.

italiancroydoner
4 years ago

Hello there
Please, correct me if I am wrong, but, just wondering, it seems that the TwentyNineteen theme is the last one with the page.php template, isn’t it?
Because I cannot find such template in the TwentyTwenty theme, and it is a real shame since, IMHO, it adds a lot of flexibility and makes customization much, much easier.
Any ideas, anyone?

Arif Malek
Arif Malek
4 years ago

Nice blog. This blog clear all my doubts and queries related to WordPress Customization.

Mikall Hill
3 years ago
Reply to  Arif Malek

YAY! So glad we could help.

wpdeveloper
wpdeveloper
4 years ago

Nice tutorial! It is a very informative tutorial. It was extremely helpful.

Mikall Hill
4 years ago
Reply to  wpdeveloper

Wow! So glad you found value in our blogs.  Thanks for visiting our site as well as taking the time to post a positive comment!

Jorge
Jorge
3 years ago

I have created a php file to customize one of my pages. I coded the template name on it and uploaded it to my wp-content/themes/astra folder. When I try to choose that new custom template on the wordpress Template dropdwoen while editing a page it doesn´t appear the name of my template. What can I do to resolve this?

sam john
sam john
3 years ago

i created my first custom page now amazing

Mikall Hill
3 years ago
Reply to  sam john

That’s awesome!! 

19
0
Would love your thoughts, please comment.x
()
x