Enabling Killer WordPress Post Formats

With WordPress 3.1 we got a new formatting tool called Post Formats. These are used to format different kinds of posts such as a status update, video, or image, and only effect posts.

This makes a little option menu in your post edit page that you can select to tag your post with the format you want. Developers then have the ability to do things with these formats dynamically, like adding elements or styling them differently based on the format.

Enable WordPress Post Formats

There are eight different post formats: aside, gallery, link, image, quote, status, audio, chat.

These can be enabled via your theme's functions file with one or 2 lines of code, or if you are too lazy, or uncomfortable with php and just want to edit css styles use the plugin I just wrote – Enable WordPress Post Formats Plugin. If you can write a little php, it would probably actually be faster to just open your functions file and write or paste it in. I wrote the plugin for those who are solid with css, but not php.

To enable the post types of your choice, use the function add_theme_support. The first argument is the feature, post-formats. The second is the array of post formats you would like to enable.

Here is the minimum code to enable all the post formats:

add_theme_support( 'post-formats', array('aside', 'gallery', 'link', 'image', 'quote', 'status', 'audio', 'chat', 'video'));

Killer Post Formats

While adding the .format-type class to the post div is all well and good, what if you want to get a little more custom with your site? Lets use the body class to add give us control over anything on the page.

// KILLER POST FORMATS
function killer_post_formats($classes)  {
    add_theme_support( 'post-formats', array('aside', 'gallery', 'link', 'image', 'quote', 'status', 'audio', 'chat', 'video'));
    function custom_body_class($classes) {
        $format = get_post_format( $post_id );
        $classes[] .= $format;
        return $classes;
    }
    add_filter('body_class', 'custom_body_class');
}
killer_post_formats();

Now you can style any design element  for each of your post formats, as well as add or remove elements.

Example Thesis Post Format Usage:

With the video post format, you can embed videos a certain way, only on posts with the video format.

function video_posts() {
    if ( has_post_format( 'video' )) {
        // EMBED CODE
    }
}
video_posts();
  1. Open and name the function.
  2. Check to see if it's a video post format page.
  3. Code for the Full Width Custom Field YouTube Player, or whatever else you want to happen on video pages.
  4. Run the function.

As you can see here, post formats are both easy to use and powerful for WordPress designers and developers. Leave a comment with examples of your post format customizations.