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
change. 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
Displays the banner that appears across the
Displays the footer that stretches across the
Shows the sidebar widget area. Twenty Twelve
|single.php||Displays a single post.|
Shows a static page. Themes often have additional
Displays the content of a post or page. Some
Displays the comments section after a post or
Shows the attachment page for images—what
Lists posts when you browse by author, category,
Lists posts when you browse a category. You can
Lists posts when you browse by tag. You can also
Lists posts when you browse by author. You can
Lists posts after you execute a search.
Displays an error message when the requested