Skip to content

Creating custom templates

Today we’re going to dig into how to create a template in our child themes.

You may not even need to do this, but in just in case, it’s important to see how we can do it without breaking WordPress. (Yes, that’s a danger, so if you do start messing with PHP, do make sure you have your site backed up first.)

Before we get started, a couple of websites for us to critique, thanks to Seanin and Nicole: King Township Public Library & teens.hpl.ca

Understanding how WordPress Templates work

I’m going to oversimplify this, that’s for sure, but I think it’s necessary to get an understanding of how WordPress generates pages, and how templates fit into that picture. Basically, WordPress works on a series of PHP files that are the programming language that builds the web pages using the content stored in a database. Those pages are built in HTML and CSS, along with images, javascripts and so on, but instead of being static HTML pages, they’re generated dynamically. PHP files are used to pull information from the website’s database to generate the page.

Templates are an important part of that process. They do two things. They will control the kind of page that gets rendered (posts, pages, search pages, author pages, category pages, tag pages, archives, and so on) and how they get presented.

I’m mostly interested in helping you control Templates for that second reason. This is for the circumstances when you have a theme that is perfect for your needs, but let’s say there isn’t a sidebar on your themes Pages. Or there is, and you don’t want it. Or you want a specially gallery page, or whatever the case may be. That’s what we’re going to tackle today.

Want more detail? Here’s a terrifying infographic showing what happens when a WordPress site is loaded in a browser.

What you need to know about templates

In WordPress there is a template hierarchy, that determines what pages are shown and in what order. Within each template, template tags may be used to pull content from the database. (For example, tags such as the_title() and the_content().) And finally there are different kinds of page templates, which change the look and feel of the site.

Here’s an overview of the Page Template Files in WordPress.

Making our own template

We can follow the handout I gave you in class for this. (Found on the OWL site if you want to save an electronic copy for later.) However, here are some resources that may help you further understand the process.

This article has a pretty good step-by-step guide on how to create your own child theme templates.

I also think the Page Template in the WordPress Theme Handbook is also worth checking out, though it’s a bit more detailed.