One of the new features in the upcoming WordPress 3.1 release is the introduction of “Post Formats”. The intent of this feature is that you can select the Format for a post, and themes can give different formats specific display styling. If you are familiar with Tumblr, then you might already have an idea of how that works. There is a specific set of formats available: Standard, Aside, Link, Status, Quote, Gallery, Chat, Image, Video, and Audio. The heart of this is in some new functions,
get_post_format(), which tell us which format is set for the current post (normally while in the loop).
There have been a few articles posted already with examples of how to add support for this in your theme. And they all made me think, “Really? That’s how they want to do it?” Because I thought that it would work a little differently…
The examples I’ve seen all follow this basic format (pseudo-code):
if ( has_post_format( 'gallery' ) ) :
// big block of HTML to format a gallery post
elseif ( has_post_format( 'video' ) ) :
// big block of similar HTML to format a video post
elseif ( has_post_format( 'image' ) ) :
// big block of similar HTML to format an image post
elseif ( has_post_format( 'aside' ) ) :
// big block of similar HTML to format an aside
// big block of similar HTML to format other posts
Those ‘big blocks of similar HTML’ inside
if/elseif blocks bother me. When you are trying to edit a section of code like this, it can be very easy to lose your place. When I first heard about post formats, I thought we were talking about a new set of files in the template hierarchy. I figured that we would be breaking out the code for each format into their own files.
You see, we also recently got this nifty new function in WordPress 3.0 called
get_template_part(). The TwentyTen theme uses this function to import different loop code depending on whether the visitor is currently viewing the homepage, a post, a page, or an attachment. The
get_template_part() function helps your theme conditionally pull in variant blocks of code pretty easily. Being able to break these pieces out makes the theme easier to maintain, because you don’t have multiple subtly-different chunks of code embedded into multiple larger template files, with conditional
if/elseif blocks to manage them.
For example, in TwentyTen’s index.php, there is a call like this:
get_template_part( 'loop', 'index' );
Which means: Look for a file named ‘loop-index.php’ in the current theme; If you don’t find it, look for ‘loop.php'; If this is a child theme, and you don’t see either of those templates, look for them in the parent theme. Each of the major templates (‘archive.php’, ‘author.php’, ‘category.php’, etc) makes a call similar to this, looking for a loop template specific to the appropriate view.
And I thought that a marriage of
get_post_format() was pretty obvious. You break your formats out into separate template files, and use
get_template_part() to decide which template to load. So when I wanted to quickly hack up a temporary theme for the WordPreh site (which you should tell all your friends about), I put my idea to the test. I tore the guts out of the loop files, and created a set of format template files. My loop simplifies from the huge
if/elseif block above down to this:
get_template_part( 'format', get_post_format() );
get_post_format() equals teh awesome!
So, when I’m displaying my list of posts, if the current post has post format ‘Link’, then we look for a file named ‘format-link.php’. If the current post has format ‘Aside’, we look for ‘format-aside.php’, if it’s a Quote, ‘format-quote.php’, regular posts look for ‘format-standard.php’, and failing all else, we use plain old ‘format.php’.
In the case of WordPreh.com, most of our posts use the ‘Link’ format, which is formatted radically differently from normal, beause I want the headline to link directly to the site URL in question (mostly Twitter statuses, sometimes blog posts) instead of the local permalink. So my ‘format-link.php’ template is quite different from most of the other ‘format-*.php’ templates.
The advantage in breaking these formats out into separate templates is that it lets me focus on just the block of code that I’m concerned with. I don’t have to hunt though 200 lines of loop code to find the right bit amonsgt a lot of similar looking bits. I only have to examine the 35 or so lines that affect the formatting for the particular view I’m concerned with. The disadvantage is that I have more loop and format files in my theme to keep track of, but I think it’s a pretty good trade-off.
So what do you theme developers think? Is this a step forward?