Getting Started with WordPress Post Formats

By / May 9, 2013 / Tips
shares

Since WP 3.1, we’ve had post formats. Most people don’t know what that is and most developers don’t make use of this great tool.

You should know about it since we’ll probably have a great increase on post formats usage with the upcoming WP 3.6 version. There we’ll find a better post formats UI, and certainly a big incentive for users to understand and use it accordingly.

Post Formats vs. Custom Post Types

Post formats allows you to select how the content should behave and display, without modifying the content type itself. So in spite of the different presentation they are all in the same big group: posts. They’ll show in the same feed and are supposed to be read as the same kind of content.

Custom post types on the other hand are more suitable, for the cases where you need different sections, you don’t want to mix those with your regular posts. The real difference here is the use, Custom Post Types are completely separated and should be used whenever you want to store any kind of content that isn’t supposed to be read by the user as posts (like portfolios, e-commerce orders, products and so on).

You can see the Post Formats as a standardized way to categorize content. So as a theme developer you’ll know for sure that items created using the “video” post format will display in a certain way, which is much better than styling a “video” category differently.

Here they are:

  • Aside – The kind of short content, that don’t need to have a title
  • Gallery – A lot of grouped images
  • Link – A link and a description
  • Image – Worth a thousand words
  • Quote – A quotation (not necessarily inspirational, a testimonial, for example would fit well here)
  • Status – Twitter-like messages
  • Video – Embedding a video
  • Audio – Embedding audio
  • Chat – Chat transcript (an interview, IM chat…)

Since they are born to be a standard you just can’t add new post formats. It wouldn’t make a lot of sense to create a standard that can be changed, right?

Setting it up

To activate it you’ll need to add a piece of code in your functions.php, like this:

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

And once you’ve done that you’re ready to rock.

It’s important to mention that you could only accept a few of them. If you theme don’t have a special format for aside, for example, you don’t need to activate it.

Using post formats

Once you’ve declared support to it, you’ll need to actually use it in your loop. There’s a simple conditional function to test the post format, the has_post_format() this is a simple example using it:

if( has_post_format(‘image’)){
   the_post_thumbnail(‘large’);
   the_title();
}

Here you’ll show a bigger thumbnail when you have an image post format. That is cool because when you use the image post format that’s what you’re looking for: to show your images.

WordPress also gives us the ability to modify it in the CSS. As many other classes, WordPress creates new conditional classes for post formats, so you can style them (if the PHP part isn’t enough, or if you are using a theme and don’t want to mess up the code).

This code is an example:

.format-status .post-title{
   display: none;
}

If you have a “status” post format you’ll mostly don’t need your title, so this will hide the entire (probably styled) post-title container.

Taking it to the next level: get_template_part

If you have completely different ways to style your post formats, you could use the get_template_part function to call new files so you can reuse it afterwards and you can also define a default file that will be called if the named file doesn’t exist. This loop will do so:

while(have_posts()) : the_post(){
   get_template_part(‘content’,get_post_format());
}

So for each post format it’ll call the content-POSTFORMATNAME.php file in your theme’s root. The good thing is that if you don’t have all the files, it’ll load the content.php file by default so you can avoid breaking your theme using a lot of different conditional includes.

Summing up

What do you think about post formats? Have you seen a good theme using it? Are you planning to use it? Let us know using the comments section!

About the Author

A web designer and entrepreneur from Itajubá (MG), Brasil. Just loves writing about obscure topics and doing some cool stuff. Get in touch at @roch_br.

9 Comments

  1. Arunshory
    May 11, 2013

    Very nice cheat sheet for wordpress blog post.

    Reply
    • Rochester
      June 4, 2013

      You’re most welcome!

      -roch

      Reply
  2. Kreativ Font
    May 13, 2013

    Yes … finally … there were even commercial plugins that did post formats …

    It’s also great you can create your own post formats …

    Thanks for the tips!

    Reply
  3. Seb Green
    May 14, 2013

    This same thing can be achieved by using categories, or tags. Also, using custom fields you can add any number of check boxes, or a drop down list to all posts allowing the users to select anything they wanted which could be used as another identifier for the post to hook onto for custom styling.

    Not sure I really see the point of this in WordPress as there are already multiple ways to do it. Why re-invent the wheel.

    Reply
    • Rochester
      June 4, 2013

      Hi Seb,

      That’s not quite the same thing. Remember this is about the best solution for a specific case (you could have different post formats, like a video and an aside in the same category). In terms of coding and theme development this is certainly a good approach (if your site requires this kind of content).

      -roch

      Reply
  4. Ben Brook
    May 16, 2013

    Agree with the categories and tags comment!

    Reply
  5. Andy Ward
    May 20, 2013

    I wish they would allow custom post formats. So for example if I’m doing a real estate site I could have “Listing” type, or I recently used WordPress for a movie theater website and I could have used a custom post format for “Movie”.

    Reply
    • Rochester
      June 4, 2013

      Hi Andy!

      They have quite a few good reasons for not doing so, but that’s mostly about standards. The same post formats should be global, but you can always use custom post types for different type of content (like in your case).

      -roch

      Reply
  6. mike
    May 27, 2013

    Hello,
    You can add custom post types very easily – get a custom post type/taxonomy ui plugin. Add a custom post type name – and that’s it, it will even create a menu item.

    You can also add custom taxonomies to your post types and filter them by your custom terms! This is simply done by filtering your “wordpress loop” (post loop) with wp_query. It is actually very fast and you can have multiple loops and taxonomies! You can also change your styling for different posts with has_term – assign custom classes to your taxonomies!

    http://codex.wordpress.org/Function_Reference/has_term
    http://codex.wordpress.org/The_Loop
    http://codex.wordpress.org/Class_Reference/WP_Query

    Reply

Leave a Reply