124 PART II • Designing and Developing WordPress Themes
used instead of the parent theme’s category.php. Chances are you need to add some CSS as
well, but all you need to do is add the necessary style to your child theme’s style sheet.
It is not just regular template file overriding that makes child themes so great. Say that you
actually just want a specific category to look and feel different. By adding category-awesome.
php to your child theme, you can target that particular category, with the slug “awesome” in
this case, without changing anything else. You could add category-awesome.php to your
parent theme as well, but that might be a problem when updating the theme, and chances are
you don’t want it to be a global thing.
THE WONDERFUL TEMPLATE PARTS
Child themes and using additional template files containing the loop are a great mix. A lot of
themes are putting the output of the loop in a content.php template file, further separating the
heavy code from the basic markup and design. The previous way of doing this was putting the
complete loop in its own template, often called loop.php (or loop-category.php, for example),
but nowadays, developers tend to keep the actual loop and its output separate. This really
helps when it comes to child themes because it lets you pinpoint a specific type of content in
your child themes, instead of overwriting a complete template file.
Suppose you want to alter the content within the loop on category archives. Your parent
theme of choice has a fully functional category.php file, which in turn includes a template
called content-category.php, with this now familiar code snippet:
<?php get_template_part( 'content', 'category' ); ?>
As you know, this will first look for content-category.php, and failing that, content.php.
Previously, you would have had to create a brand-new category.php template file to get to the
loop and its output used there, but no more. If you want to alter the actual loop in your child
theme, you still have to alter category.php, but if it is just the loop output you’re interested in,
there’s a better way. If you add a content-category.php template file in your child theme, you’ll
be able to override the actual loop output. Because the child theme’s file will take precedence,
the content-category.php file that your parent theme’s category.php wants to include will be
pulled from your child theme instead (assuming you’ve put it there, of course). In other
words, you can get to the loop included in the parent theme’s category.php without having to
put a complete category.php template in your child theme — just the loop. Sweet, huh?
The same technique obviously works just as well for other template files that are included this
way. Say that you’ve got a menu that differs around the site. You’re including it with get_
template_part(), looking for nav-X.php and defaulting back to nav.php. The following
code would look for nav-archives.php and, failing that, settle for nav.php:
<?php get_template_part( 'nav', 'archives' ); ?>
In your child theme, you could easily target nav-archives.php if you wanted to, just like you
can target specific content output. This way, you can change parts of the site that aren’t just the
loop as well, using child themes, which will save time and effort when maintaining the theme.