WordPress: How to create a custom page template?

2013-09-26 WordPress

Custom page template gives you ability to have an unique design for specified page(s) that should look different than default template like for example page.php. In this article I will show you how to create new template and assign it to custom page.

The most important things that you should now dealing with WordPress templates is general construction syntax. Correct and raw template should have functions:

  • get_header()
  • get_footer()

included in it’s content. Thanks to that, your template file will be valid and render correctly.

Create a new file save it into:

/wp-content/themes/[YOUR_THEME_DIRECTORY]/

The name of this file is as much important as it’s content. Here’s why. File’s name should start with phrase template and a dash. Next part depends on you – it can be an ID or a slug name. For testing purposes call the file:

template-knowledge-article.php

Now open this file and paste the following code:

<?php
/**
 * Template Name: Knowledge Article
 *
 *
 */
get_header();
?>

<!-- Your content goes here... -->

<?php get_footer();

Look carefuly at line number three – Knowledge Article phrase is the value, that will be shown when you will be choosing from a list, which template assign to a custom page. Save the file and log into your WordPress dashboard. Now go to Pages menu and pick one of the existing pages. In section Page Attributes, open Template list and change the value from Default Template to Knowledge Article. Yes – you’re right. That’s the name from third line of your newly created file. Look at the screen to see the list, about which I’m talking right now. Save your settings.

You’re done – now custom page has different template than the default one. Of course it’s blank. Now it’s up to You to add more content. You can grab some stuff from page.php and then add some additional code.