| WordPress

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

tutuploadsmedia_1336168606733.png

  • 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

tutuploadsmedia_1336173432524.png

  • 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

tutuploadsmedia_1336170329407.png

  • 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

tutuploadsmedia_1336173189966.png

  • 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

tutuploadsmedia_1336171527227.png

  • Go to Pages > Add new.
  • Choose My-New-Page-Template from the Page Attributes.

tutuploadsmedia_1336173131737.png

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.