How To Create WordPress Custom Page Template

Have you ever wanted to create a custom page for a WordPress page, or just to create a page to display a custom code without your website header and footer. The answer is a custom template.

What is a WordPress custom page template and how can we create it?

A WordPress custom page template is a php file created inside your theme folder. In this file you have to put the following code in top of the page in order to get recognized as a template from WordPress admin panel.

<?php /* Template Name: NewTemplate */ ?>

After setting up this php file, in order to create a page, login in your admin panel, set the title of the page and choose the template we created from the dropdown menu at right of screen.

WordPress Template Section

The page is setup now. Click view page and your template will show up.

Now you can decide if you want to receive data from WordPress or just create your custom code to receive data from another source.

If you want to receive content from WordPress page, just create a loop and style the template as you wish. Just don’t forget to include header and footer in your custom template. Here is an example:

<?php /* Template Name: NewTemplate */ ?>
while ( have_posts() ) : the_post();
// write your code here

If you want a custom page with codes not related to WordPress, without header and footer, delete everything from the custom page except the page template name that tells WordPress that this is a custom page.
Don’t forget to leave WordPress content empty in this case. Write your own custom code and when you access the page created from WordPress admin panel, your custom code will execute. Here is a example of a page that displays the sum of two numbers:

<?php /* Template Name: NewTemplate */ ?>
$num1 = 1;
$num2 = 5;
echo $num1 + $num2;

This page will display a page that prints the sum of two numbers. You can write whatever you want in here.

Hope this article helped you to understand WordPress custom pages.
If you liked this article, please share or tell a friend about this page.

Leave a reply