How to 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
- 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
- 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
- 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
- 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
- Go to Pages > Add new.
- Choose My-New-Page-Template from the Page Attributes.
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.
Thank you for this article! It is just what I needed. I appreciate the list of reserved file names too.
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!
I can’t figure out what I’m doing wrong. This seems super simple but it’s just not working for me.
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
I can able to select the template that i made
Thank you very much! You saved my time.
wordpress so nice
its febraury 2020 and this arcticle give me what i really wanted thanks
Also, singular.php is reserved.
Thanks for this helpful Blog.
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.
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?
Nice blog. This blog clear all my doubts and queries related to WordPress Customization.
YAY! So glad we could help.
Nice tutorial! It is a very informative tutorial. It was extremely helpful.
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!
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?
i created my first custom page now amazing
That’s awesome!!