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

(avery) #1

CHAPTER 6 • Advanced Theme Usage 135


But first, you need to understand the more obvious individual styling technique. I’m talking
about Pages, which can be easily styled to act in any way you like because all you really need
to do is create a Page template that behaves in the way you want. You can take this even
further than is possible with blog posts by loading different sidebars, headers, footers, or
whatever you need, really. The strength of the Page template is that it can be set up any way
you want, and all you need to do is create it and choose it for your Page.


The same applies to category and tag listings. If you want to add a header graphic to a specific
category, for example, all you need to do is alter that particular category’s template file. You
may remember that category-X.php takes precedence over category.php (which in turn beats
first archive.php and then index.php) and that X is the category ID or category slug. So
category-37.php would be the template file whenever a listing of posts in the category with ID
37 is called for, just as category-cows.php is the template file of choice when viewing the Cows
category. Hence, you can just edit the category-37.php (or category-cows.php) template file to
reflect how you want that listing to look.


Don’t forget about the description for tags, categories, and whatever custom
taxonomies you might have created. Not only is the description useful on archives,
but it can also make these pages more accessible to search engines. Refer to
category_description(), tag_description(), and term_
description(), respectively.

In short, it is easy enough to add a little extra styling to the parts of your site where there are
template files to control that style. Consult Chapter 4, “WordPress Theme Essentials,” for more
information on which template file is loaded when, and take that information into account
when shaping up your site.


STYLING THE POSTS


Styling the individual posts may be a bit trickier. They are all governed by single.php, which
means that you need other means of making them stand out when needed.


Enter the the_ID and post_class() template tags, which go in your .post container,
like this:


<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>




You’ll find this (or something similar, at least) in most WordPress themes. The the_ID()
template tag returns the post ID, giving the article container the ID of post-X, where X
is the returned ID. That means that this is a way to catch a specific post and make it behave
the way you want. ID 674 would hence give you an article with id="post-674" to play
with. Style it any way you like.

Free download pdf