I use the Thesis theme for my WordPress blog. Thesis is much beloved in the WordPress community because it allows a high degree of customization using its two pages of configuration menus, so even if you know nothing about PHP or CSS you can do a lot to customize your blog. And once you’re ready to start messing around with a little bit of code, you can do really amazing customizations using the Thesis system of hooks and functions.

I recently used the Thesis configuration system to change the way my blog displays teasers, making use of WordPress conditional tags. Most of my home page, and all of my archive and category pages, consist of teasers. It’s incredibly easy to decide which elements to include in your teasers by checking the appropriate boxes under Teaser Display Options on the Thesis Design Options page, and to order those elements by dragging and dropping within the list. That’s how I configured my teasers to show the category of each post, right above the title of the post.

The Teaser Display Options in Thesis… …let you display category names.

I did encounter one small hitch on teasers for posts that are in multiple categories: when Thesis displays the category for a teaser, it displays the “primary category”. With a little experimentation, I figured out that WordPress defines the primary category as the category that is first alphabetically. Since I was using a category called “Featured” to select posts for inclusion in my 3 featured teasers at the top of the page, a number of teasers showed “Featured” instead of the category heading I wanted to display (Lifestyle, Productivity, Relationships, Self). Since the “Featured” category works strictly behind-the-scenes, the easiest way to resolve the problem was to rename the category: I now call it “Zone”, which means that any post in my “Zone” will display an alphabetically higher category (Career, Lifestyle, Self, etc.) and Zone never appears visibly on the site.

I made the alphabetization trick work for me in another way, too. By renaming my “Harvard Business Review” and “Oprah.com” categories to “AT Harvard Business Review” and “AT Oprah.com”, I ensured that any posts that I originally published on those sites would be visibly identified in the teaser.

But I had one more problem: my category pages looked stupid. On the Community page, every single teaser said Community. On the Relationships page, every single teaser said Relationships. The exception were my Oprah and HBR posts: those said At Oprah.com or At Harvard Business Review just above the post headline. That looked pretty neat…except on the Oprah.com and HBR pages themselves, where again they looked stupid because the At Oprah.com appeared above every single post title.

With a little experimentation, I arrived at a two-part solution, adapting a PHP code snippet for formatting teasers that I found on the DIY Themes site (the folks behind Thesis). Before using these snippets on your own site I recommend getting a basic understanding of the Thesis hooks and functions system so you have a vague sense of what you’re doing here.

Here are the steps I used:

1. Under Design Options/Teaser Display Options, I unchecked “primary category” so that the category name no longer appeared in any teasers.

2. Using the Custom File Editor, I added the first snippet to custom_functions.php.
This snippet is very simple. It displays all category names on teasers that appear on the home page and monthly archive pages.


//show category names in home page teasers
function my_teaser_byline() {
     if (is_home() | is_date()) {
global $thesis_design;
     $categories = get_the_category();
     echo '' . $categories[0]->cat_name . '' . "\n";
}
}
add_action('thesis_hook_before_teaser_headline', 'my_teaser_byline');

3. I added the second snippet to custom_functions.php so that category names would appear on any posts in the “At Oprah.com” or “At Harvard Business Review” categories [if (in_category(array(‘hbr’,’oprah’))] unless the page displayed was the category page for Oprah.com or HBR.

To customize this snippet for your own site, just replace ‘hbr’ and oprah’ with the category slugs (not category names) of the categories you want to show in your teasers.


/show Oprah and HBR in teasers on category pages
function my_teaser_byline_categories() {
     if (in_category(array('hbr','oprah')) && !is_category(array('hbr','oprah'))) {
global $thesis_design;
     $categories = get_the_category();
     echo '' . $categories[0]->cat_name . '' . "\n";
}
}
add_action('thesis_hook_before_teaser_headline', 'my_teaser_byline_categories');

And that’s all it took! I now have teasers that display differently on different parts of my site, as follows:

My home page shows category names for all teasers; my monthly archives do, too.

The teasers on category pages only show the category name when it’s used to identify a post that appeared on Oprah.com or Harvard Business Review, as follows:

And my HBR and Oprah.com archive pages don’t show category names at all.

Enhanced by Zemanta