Now that we've got the Loop working in our theme, it's time to start breaking the theme down into template files, which will help us make sure edits flow consistently across all of the various aspects of the theme.
A good rule of thumb for separating markup and code into its own template file is:
The most common template files we'll look at first are the header.php
, footer.php
, and sidebar.php
template files. Each of these template files will be used in various types of pages on the site.
The header should be the same for a post or an archive as for a static page. The footer should remain consistent across all types of content on the site. If we update or change the header or footer, we'll want that change to appear everywhere it's used and not have to edit two or three different template files. The same goes for the sidebar—we may want it on several types of pages, or not. This is easy to manage if it's contained in its own file, and included where needed into other template files which control different content types.
These three template files (mentioned in the previous section) are so common that they have their own include template tags in WordPress, namely get_header
, get_sidebar
, get_footer
. The tags are used like so in your template file:
<?php get_header(); ?>
—to call the header.php
file<?php get_sidebar(); ?>
—to call the sidebar.php
file<?php get_footer (); ?>
—to call (yes, you've guessed it) the footer.php
fileLet's start by creating the header.php
file and adding the tag to include that in our index.php
file.