As a freelance web designer, I often run into projects that require a variety of layouts and unique page templates. As I begin to embrace Gutenberg (see my post on creating Gutenberg blocks) more fully, I'm sure the number of unique templates will begin to decrease. But for now, I still need to create several unique page templates for my custom development projects. In this post, I'll show you how to do that if you have access to FTP. If you're in a pinch and only have access to the WordPress backend, I'll also show you how to create a new page template using a little hack I learned over the years! Let's get started!
Create Page Template Using FTP
- Create a new PHP file. Name it something memorable that describes the page template. In this example, I'll create a file called about-us.php.
- Copy/Paste the default code from your page.php file. I use the Underscores starter theme and highly recommend it. But it doesn't matter which one you choose.
- On the very first line, include the code snippet below. This defines what the page a unique page template.
- Save and upload your new PHP file in the root folder of your theme. This is the same folder that contains style.css and other page templates.
- Voila! You've created a new page template.
<?php /* Template Name: About Page */ ?>
No FTP Access? No Problem!
If you're in a situation where you can't get access to the FTP but have access to WordPress, you can still create a new page template. To do this, follow these simple steps:
Create a New PHP File
- Go to the the theme editor by clicking "Appearance > Theme Editor".
- Find the header.php file and click edit.
- Insert the code snippet below on the very first line of the header.php file. Be sure to change your theme and file name.
- Refresh any page of your website in the browser. This will execute the php touch action and create the the new PHP file in the directory you've specified.
- Go back to your Theme Editor and you'll now see the new PHP file in your directory listing.
<?php touch('wp-content/themes/your_theme_directory/about-us.php');?>
Create the Template File
- After you've created the about-us.php file (or whatever you called it), edit the file in your Theme Editor.
- Insert the following code snippet on the very first line to create the page template. Be sure to replace About Page with whatever template you are creating.
- Save and enjoy your new page template!
<?php /* Template Name: About Page */ ?>