Dougal Campbell's geek ramblings

WordPress, web development, and world domination.

Smarter Post Formats?

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, has_post_format() and 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):

while ( the_loop() ):
    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
    else :
        // big block of similar HTML to format other posts
    endif;
endwhile;

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_template_part() and 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:

while ( the_loop() ):
    get_template_part( 'format', get_post_format() );
endwhile;

get_template_part() plus 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?

About Dougal Campbell

Dougal is a web developer, and a "Developer Emeritus" for the WordPress platform. When he's not coding PHP, Perl, CSS, JavaScript, or whatnot, he spends time with his wife, three children, a dog, and a cat in their Atlanta area home.
This entry was posted in WordPress and tagged , , , , , , , , , , , , . Bookmark the permalink.

65 Responses to Smarter Post Formats?

  1. Pingback: Tweets that mention Smarter Post Formats? #wordpress -- Topsy.com

  2. Pingback: Smarter Post Formats? Use Carrington | alexking.org

  3. Pingback: WP'it

  4. Pingback: Wordpress Post Format

  5. Pingback: Michael Fields » My First Post Format

  6. Pingback: » Additional Resources

  7. Pingback: WordPress Post Formats  | prosoxi.com

  8. Pingback: Mini entrada | DEMO EMPLATE v10

  9. Pingback: WordPress 3.1 (Reinhardt) Features: New CMS Capabilities and More | Free Wordpress Themes at KiloWordpress.com

  10. Pingback: How to display post formats?

  11. Pingback: Creating Post Formats | (Re)Learning Wordpress Development

  12. Pingback: ? ?? | wordpresscoop.co.krwordpresscoop.co.kr

  13. Pingback: Post Formats and Content Templates in the Toolbox Theme | ThemeShaper

  14. Pingback: Function Reference/get post format | ???

  15. Pingback: ? ??, ?? ??, ???, ??, ???, ??, ??, ???, ???, ??, aside, gallery, link ?

  16. Pingback: Step by Step Guide to WordPress Post Formats | Shade Tree Sites

  17. Pingback: WordPress?????get_post_format – Kraddy WordPress

  18. Pingback: WordPress?????get_post_format | WPFarmer

  19. Pingback: WordPress 3.1 and Post Formats - Curtis McHale

  20. Pingback: Smarter Post Formats? | Hugo Ferreira

  21. Pingback: Outstanding New features of WordPress 3.1(Reinhardt) - WPArena

  22. Pingback: Post Formats | WordPress World

  23. Pingback: Post Formats - LitePress ????

  24. Pingback: ???? – ??

  25. Pingback: Display/query post formats - Code Solution

  26. Pingback: Post Formats – ??.SHOW

Leave a Reply to Keisa Cancel reply

%d bloggers like this: