How to Create Custom WordPress Template Posts, Pages, Headers, Footers and Sidebars

How to create new WordPress templates for your theme including posts, pages, headers, footers and sidebars.

To change the WordPress home page

  • edit the index.php in the main theme
  •  if using a child theme, just copy and then edit the file in your child theme folder
  • alternatively you can create/edit front-page.php  – this will take priority over the index.php

To make a specific WordPress custom template page

  • create the page in WordPress front end via the editor
  • get the Page ID – which you can find whilst editing the page – look for the format in the url field “post.php?post=2092&action=edit” – here the ID is 2092
  • copy page.php and rename it page-2092.php
  • if using a child theme put the page-2092.php in the child theme folder
  • change the html/php to suit
  • if another template is used with a different name  – then copy that and rename it with the same initial name and append the number ‘templatename-2092.php

Instead of using the ID number you can also use the URL slug instead, so if you had a page /about-us you could use a template page-about-us.php

These principles also apply to category and tag templates.

To make global WordPress custom template pages

To build a custom page template and use it across any page you create from within WordPress dashboard, copy and edit the page.php template and say rename it to page-new.php, declare it as a template by adding this php declaration at the top of the file:

<?php
/*
Template Name:News Page
*/
?>

Then access it from within the visual editor and select the new template.

custom wordpress page templae

To make a custom template for a single WordPress post

– this is done quite differently and involves a couple of different ways to do it if you just want to make subtle template changes I recommend installing a plugin that allows you to use the same method as creating page templates. If your needs are more complex the other method is to create and register the custom post types and have them in a separate menu outside of the regular posts – this covers that way.

So the easier way is to install and activate  a Custom Template Plugin that allows the functionality – there are a couple of these, this one seems to be popular and updated recently.

Creating the Post Temple

The post template is taken from the single.php template.

  • locate the single.php and make a copy and call it something like single-newspost.php
  • open the copied template
  • make a php statement in the head of the file and name the template, in this case named “News Post”
<?php
/*
Single Post Template:News Post
*/
?>

single post template

– It will then be available from within WordPress dashboard in the Posts edit page in a widget called ‘Single Post Template’ and can be selected to use. Initially the widget is below the visual editor you can drag it to the right column so it appears in the same area as the Page Custom Template dropdown.

To make custom headers, footers and sidebars (2 ways)

Easier way

  • copy and rename header.php, footer.php or sidebar.php
    – e.g header-special.php
  • edit the new file
  • reference the new file from another template by appending the suffix name, like so:
get_header('special');

Slightly longer way

  • copy and rename header.php – e.g specialheader.php
  • in the custom page or new template, reference the specialheader.php by changing:
<?php get_header(); ?>

to

<?php include (TEMPLATEPATH . '/specialheader.php'); ?>

If you are using a child theme, you need to call ‘get_stylesheet_directory() .’

<?php include (get_stylesheet_directory() . '/specialheader.php');  ?>

2 Comments

  1. dk on April 19, 2016 at 11:28 am

    looks good… thanks

  2. abhishek on May 12, 2015 at 2:39 pm

    I really don’t understand this

Leave a Comment





%d bloggers like this: