What Is Template in WordPress? How to Use Templates in WordPress

With over 64 million websites built on WordPress, it‘s the world‘s most popular CMS. Templates are the backbone of any WordPress site.

As a web developer with over 15 years of experience building on WordPress, I‘ve seen firsthand how important templates are. Understanding templates is key to customizing and designing WordPress sites.

In this in-depth guide, I‘ll explain everything you need to know about WordPress templates as an expert user or developer.

What Exactly is a Template in WordPress?

A template is a .php file that contains a mix of HTML, PHP, and CSS code. It generates the output that appears on the front-end of your WordPress site.

Templates only work when an active WordPress theme is installed. The theme houses all the template files together in a central location.

Some common templates you‘ll find in most WordPress themes include:

  • index.php – The main template for displaying posts, archives, search, etc.
  • single.php – Used for viewing single posts.
  • page.php – Displays content for single WordPress pages.
  • header.php – Outputs the header HTML.
  • footer.php – Outputs the footer HTML.
  • sidebar.php – Generates HTML for the sidebar content.
  • comments.php – Controls the display of comments.

These core template files work together to build all the different parts of your WordPress site that visitors see.

The name of the template tells WordPress exactly which part of the site to build. For example, single.php outputs just a single post‘s content area.

Understanding the Difference Between Templates and Themes

I sometimes see confusion around the difference between templates vs themes:

  • A theme is a collection of templates, stylesheet files, javascript, images, and other assets. It controls the overall design and front-end display of a WordPress site.

  • A template is an individual .php file that lives within a theme. The template outputs markup for specific pages and components.

The theme houses groups of templates together in one place. Without the main theme infrastructure, the templates would have no styling or site functionality.

So you can think of templates as building blocks that themes combine to generate full web pages.

As a real-world analogy, consider building a house. The theme is the entire house. Templates are like the rooms – kitchen, bedroom, living room – that get pieced together to build the house.

How Do Templates Work in WordPress Themes?

Templates provide the core HTML structure for displaying all the content across a WordPress site.

For example:

  • The header template generates the HTML needed for the top header section.
  • The sidebar template outputs the HTML for the sidebar widgets.
  • The footer template displays the bottom footer content.

WordPress combines the output of different templates together to dynamically build each page.

Templates contain a mix of regular HTML, CSS, and PHP code:

// Example template code

<?php get_header(); // Gets header template ?>

<div class="page-content">

  <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    // Template loop to display posts

  <?php endwhile; else: ?>

    // No posts found

  <?php endif; ?>

</div>

<?php get_sidebar(); // Gets sidebar template ?>

<?php get_footer(); // Gets footer template ?>

The PHP fetches data from WordPress and custom functions to display dynamic content. The HTML structures the overall page layout.

How to Find Template Files in Your WordPress Theme

To view or edit templates, you first need to access the theme file location. There are two ways to locate theme templates:

Via FTP

Connect to your hosting server via FTP using a client like FileZilla. Navigate to the /wp-content/themes/your-theme folder.

This houses all the template files for that theme. You can download them for editing.

Through the WordPress Theme Editor

From your dashboard, go to Appearance → Theme Editor. On the right side, you‘ll see a list of templates associated with your active theme.

Click any file name to view or edit the code in the center column. This lets you customize templates through the admin.

The WordPress Template Hierarchy: A Cascade of Templates

Here‘s an insider tip that lets you build advanced custom templates…

WordPress uses a template hierarchy system to determine which templates load on each page. The hierarchy acts as a cascading set of rules that WordPress follows to find the right template file.

It first looks for very specific template names like single-post.php, then falls back to more general templates like single.php, and finally index.php as the catch-all template.

This cascade allows themes to create very customized templates:

WordPress Template Hierarchy

Following the template hierarchy gives developers granular control over every page‘s markup while avoiding duplicate code.

I suggest studying the full WordPress template hierarchy to master WordPress templates.

Creating Custom Page Templates in WordPress

Many premium WordPress themes include special page templates for things like landing pages, contact forms, pricing tables, etc.

You can create your own custom page template files by adding new .php files to your active theme directory.

Start by adding a template header like:

<?php
/*
Template Name: My Custom Template
*/
?>

This template declaration tells WordPress to make it available in the page editor dropdown.

Then build out your template structure using HTML, PHP, and WordPress template tags. Add your own custom layout and functionality.

Save your template and you‘ll be able to select it from the Page Attributes when editing a page or post. This lets you create unique layouts for specific content.

In Closing – Templates Are the Backbone of WordPress

Learning how templates work is essential for designing and customizing your WordPress site‘s front-end. Templates provide the foundational structure that themes then expand upon.

WordPress combines templates together based on the template hierarchy rules. This gives developers flexibility for customized single post/page layouts.

Editing existing templates or building new custom page templates gives you full control over your site‘s markup and output. Just be sure to follow WordPress coding standards.

I hope this guide gave you a solid understanding of the role templates play in WordPress. Let me know if you have any other questions!

Written by Jason Striegel

C/C++, Java, Python, Linux developer for 18 years, A-Tech enthusiast love to share some useful tech hacks.