post format ui

WordPress 3.1 is just around the corner so let’s take a quick look at one of the new features that we’ll get to take advantage of with the new version.

What are Post Formats

Basically post formats are little bits of meta information meant to change the display of the posts. So a link post can display different from a long format post, or a gallery can look different from a post with a link. Typically we’ve achieved this with categories so having a ‘link’ or ‘video’ category then using conditional tags to change the display of the content. Two great examples using the category method (at the time of writing since post formats don’t exist and I’m assuming) are Digging into WordPress and Matt Mullenweg’s blog. On Digging into WordPress you’ll see long format posts and links to other great resources so they have two ‘post formats’. Matt uses many formats including links, long form, video, and photo.

Examples of Post Formats
Examples of Post Formats

We already have Post Types so why do we need this?

The biggest issue with custom post types it that if implemented in a theme they are tied to that theme. So when your user switches to a new theme they can loose your custom post type. Sure if you’re doing a custom theme for a client you can make your custom post types a plugin and that does mitigate some of the issue but post types are still specific to that plugin so you’ve lost some data portability.

Custom Post Types should probably really be called Custom ‘Content’ Types since they don’t really deal with posts. When you think of them you should think of items like products, shows, and employees instead of podcasts (as an example). You’d find all of them in the posts section of WordPress and they’d all show up in your normal feed.

Post Formats are just that a way to label your posts as a special format for display.

Post Formats allow you to register support for something that WordPress knows about. If you support the gallery post format then WordPress can recognize that and do something special about it or with the content in question. Basically it’s a standardization of data to increase portability and compatibility for both themes and plugins.

Alright I’m convinced how to I use them?

Remember when I wrote this Post Formats were still in beta so go go read the Codex for the most up to date information. I’ll do my best to update this if changes happen.

To take advantage of the built in formats in your theme you or plugin you need to register support for them just like you do with post_thumbnails(). So throw the code below into your functions.php file:

[php]
add_theme_support( ‘post-formats’, array( ‘aside’, ‘gallery’ ) );
[/php]

The example above would tell WordPress that your theme supports the aside and gallery post format.

Once you’ve added support for them in the theme you need to actually do something with them in the theme. With the addition of post formats we also get the conditional tag has_post_format() (no page content at time of writing) which, you guessed it, does stuff if the post has the specified post format. So let’s quickly tell the theme to do different things if the posts contain the formats listed above.

Edit: Check out a great post on an alternative method for getting different loops for post formats.

[php]
if ( has_post_format( ‘aside’ ) ) {
// display aside format
} elseif ( has_post_format( ‘gallery’ ) {
// display gallery format
} else {
// all other formats
}
[/php]

Now we’ve got three different templates in operation, aside gallery and one to catch any other post format that comes along.

What if we’d like to extend post formats? Officially WordPress says no to extending post formats. They want to preserve data portability and a ton of developers running all over adding their own post formats doesn’t encourage that. Of course it’s simply code so it is actually possible to extend them, but I’d recommend staying away from it.

Don’t forget that if you’re plugin or theme needs to be backwards compatible with previous versions of WordPress you need a bit more code. First you need to regsiter_taxonomy() for post_type then insert the term for the post format.

[php]
wp_insert_term( ‘post-format-aside’, ‘post_format’ );
[/php]

What’s Missing?

In my opinion one thing missing is audio post formats (though @otto42 has attached a patch for them). Given how often WordPress is used to run podcasts it just seems like a something that should be included out of the box. I’d also love to see a ‘code’ format for posts that are mainly code like you’d find on the blog of mfields. I realize ‘code’ is a bit developer specific and won’t suit the bigger community so that’s a bit more of a wish really.

If you’re excited to play with Custom Post Types in WordPress setup a development environment and install the WordPresss Beta Tester plugin. Happy hacking.

Further Reading

This is the stuff I dug around through while writing this, in addition to links in the article above.