Thord Daniel Hedengren - Smashing WordPress_ Beyond the Blog-Wiley (2014)

(avery) #1

CHAPTER 4 • WordPress Theme Essentials 97


◾ Custom taxonomy archives: taxonomy-X-Y.php where X is the taxonomy slug and Y is
the term slug (for example, taxonomy-person-waldo.php would be the term “waldo” in
“person”), then taxonomy-X-php where X is the taxonomy slug, then taxonomy.php, then
archive.php, and finally index.php.
◾ Custom post type archives: archive-X.php where X is the slug for the custom post type,
then archive.php, and finally index.php.
◾ Author archives: author-X.php where X is the slug, then author-Y.php where Y is the ID,
then author.php, archive.php, and finally index.php.
◾ Date archives: date.php, then archive.php, and finally index.php.
◾ Search results: search.php and then index.php.
◾ 404 (Not Found): 404.php and then index.php.
◾ Attachments: Per MIME type, such as image.php for images, video.php for videos, text.
php, and so on; then attachment.php; single-attachment.php; single.php; and finally
index.php.

Remember, a theme doesn’t need to include all the possible template files. In fact, you
shouldn’t use more of these than you really need to because that only means that you’ll have
more files to maintain should WordPress change or if you want to alter something in your
design.


Some of these templates may be confusing. Take a look at the category.php template, for
example. By default, this template displays the post listing from any given category. However,
should there be a category-X.php template, where X is the ID of the category in question, that
template file will be used rather than the category.php template. The same goes for tags: tag-X.
php is displayed before tag.php. The same goes for category and tag slugs, so the category
Monkeys with the slug monkeys and an ID of 42 can be reached at category-monkeys.php
as well as category-42.php.


In short, WordPress will look for a specific template file, and if that one isn’t present, it will
fall back to the next best template file, and lacking that, fall back again, and so on.


Keep in mind that page.php and Page templates are actually two different things. You may
have noticed that you can set a template when creating WordPress Pages. These are Page
templates, not controlled by page.php but rather sporting their own header information
much like style.css. They can be very useful, so you’ll play with them in a little while.


THE TEMPLATE HIERARCHY


Now that you know what the template files are, you need to know which one is loaded when
and what happens if there is no template at all. As you know, index.php is the fallback
template for everything, and you can settle for using only that. Figure 4-8 illustrates the
hierarchy of the template files. For a more comprehensive version, along with conditional tags,
see the WordPress Codex at http://codex.wordpress.org/Template_Hierarchy.

Free download pdf