Codeable info

WordPress 3.1 Post Formats

Posted on by in WordPress Tutorials

With the release of WordPress 3.1 comes a lot of sweet new features, particularly the addition of custom post formats, which allows us to essentially create as many different post “templates” as we wish. So rather than relying on a plugin like Simple Post Templates, we’ll be able to natively set up various types of post layouts.

For example, we could easily create a post format for video posts, one for testimonials, one for photos, etc. Each one of these post formats could then be styled and organized in anyway you so choose.

Setting up post formats is easy. First, in functions.php:

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

This will enable custom post formats in your theme. The array is a list of all the different formats we wish to create.

Now, if you go to your post editor, you will see something like this:

WordPress 3.1 Post Formats
Image src: http://lisasabin-wilson.com/wordpress-3-1-post-formats-reference

Next, is to customize our template files to reflect our selected post format on the front end.

There is a simple WordPress condition tag that we can use to check for post formats.

1
2
3
if ( has_post_format( 'aside' )) {
	// code for aside format here
}

This check would return true if the current post had a format of aside, and false otherwise. So using this method, we can very easily set up some formats.

1
2
3
4
5
6
7
8
9
if ( has_post_format( 'aside' )) {
	echo '<div class="aside">' . the_content() . '</div>';
}
elseif ( has_post_format( 'photo' )) {
	echo '<h3>';
	echo the_title();
	echo '</h3>';
	echo the_content();
}

The idea is that you customize the kind of HTML / CSS that is used depending on the format the post has.

So go have fun with custom post formats!

Pippin

Comments Closed

Codeable info