By Focusoncode | May 29, 2018

Adding a custom page template in WordPress is relatively easy. It needs just a couple of minutes to create. Once created, the new custom page template can be chosen from admin panel, before or after adding a new page.

Admin Panel -> pages -> add new -> Choose template

The “Choose template” can be found on the right side of page content, under the page attributes section.

Adding custom page template

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 custom template from WordPress admin panel.

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

Choosing custom page template

After creating this php file, 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.

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

Adding content to the custom page template

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 */ ?> <?php get_header(); if(have_posts()): while ( have_posts() ) : the_post(); // write your code here endwhile; endif; get_footer(); ?> 1 2 3 4 5 6 7 8 9 10 <?php /* Template Name: NewTemplate */ ?> <?php get_header ( ) ; if ( have_posts ( ) ) : while ( have_posts ( ) ) : the_post ( ) ; // write your code here endwhile ; endif ; get_footer ( ) ; ?>

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 */ ?> <?php $num1 = 1; $num2 = 5; echo $num1 + $num2; ?> 1 2 3 4 5 6 <?php /* Template Name: NewTemplate */ ?> <?php $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.