Introducing the Template Files

Every theme uses a slightly different assortment of templates. The WordPress staple
Twenty Twelve uses a fairly typical set of about 30 templates.
You can recognize a template by the fact that its filename ends with .php. Although
template files hold a mix of HTML and PHP, the .php extension tells WordPress that
there’s code inside that the WordPress engine needs to run before it sends the final
page to a browser.
Even though a template is just a mix of HTML and PHP, understanding where it fits
into your site can be a bit of a challenge. That’s because every page WordPress
stitches together uses several template files.
For example, imagine you click through to a single post. WordPress has a template,
called single.php, that creates the page on the fly. However, single.php needs help
from a host of other templates. First, it inserts the contents of the header.php
template, which sits at the top of every page in a WordPress site. The header.php
file takes care of basics, like linking to the style sheet, registering some scripts,
and showing the header section, complete with the top-level menu. (Some themes
farm out the menu-creation work to yet another template file, but Twenty Twelve
doesn’t go that far.)
Next, the single.php file adds the Previous and Next navigation links to the post,
and then calls out to another template to display the actual post. If it’s a regular
post, it uses content.php, but the Twenty Twelve theme has a number of specialized
alternatives for different post formats. For example, an “aside” post (page 203) uses
the content-aside.php template, a “status” post uses content-status.php, and so on.
Finally, the single.php template ends by calling three more templates into action. The
comments.php template creates the comments section, the sidebar.php template
adds the widgets on the right, and the footer.php template ends the page.
If you’re going cross-eyed trying to follow this template assortment, Figure 13-14
shows how it all breaks down.
At first glance, this system seems just a bit bonkers. How better to complicate life
than to split the code for a single page into a handful of template files that need to
work together? But in typical WordPress fashion, the design is actually pretty brilliant.
It means that a theme designer can create a single template file that governs
a repeating site element—like a header—and use it everywhere, without needing to
worry about duplicating effort or being inconsistent.

When you edit theme styles, your first challenge is finding the right style rule to
. When you edit templates, the first challenge is finding the right template
file to modify
. Table  can help you get started. It describes the fundamental
templates that almost every theme, including Twenty Twelve, uses.

Keep in mind, however, that themes commonly add extra templates to create the
formats for different types of posts and pages, and to handle special formatting (for
example, to create different layouts that move the sidebar around). You may also
decide to add extra templates of your own (for example, to change the way your
site presents specific categories or authors.




This is a theme’s main template, and the only one
that’s absolutely required. WordPress uses it if
there’s no other, more specific template to use.
Most themes use index.php to display a list of
posts on the home page.


Displays the banner that appears across the
top of every page. Often, header.php includes a
navigation menu.


Displays the footer that stretches across the
bottom of every page.


Shows the sidebar widget area. Twenty Twelve
also has a more specialized sidebar-front.php
template that creates the footer area for pages
that use the Front Page template.

single.php Displays a single post.

Shows a static page. Themes often have additional
page templates that let you create different
“flavors” of pages (page 233). For example,
Twenty Twelve adds a full-width.php template
that creates a page that doesn’t include a sidebar
and a front-page.php template that includes a
two-column widget area in the footer.


Displays the content of a post or page. Some
themes create many different content templates,
for different types of posts and pages. Twenty
Twelve, for instance, has eight content templates.
An ordinary post uses content.php, a page uses
content-page.php, and special post formats
(page 202) use a corresponding template (for
example, asides use content-aside.php).


Displays the comments section after a post or


Shows the attachment page for images—what
you see when you click a picture in a post, if
you’ve set the Link To setting (page 336) to
Attachment Page.


Lists posts when you browse by author, category,
tag, or date. Or you can use one of the four more
specific templates listed next.


Lists posts when you browse a category. You can
also create templates that target specific categories,
like category-tea.php for a category with
“tea” as its simplified name


Lists posts when you browse by tag. You can also
create templates that target specific tags, like


Lists posts when you browse by author. You can
also create templates that target specific authors,
like author-grabinksy.php.


Lists posts after you execute a search.


Displays an error message when the requested
post or page can’t be found.

Introducing the Template Files

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s