Post formats in WordPress are pretty cool. According to the Codex, “A Post Format is a piece of meta information that can be used by a theme to customize its presentation of a post.” In my mind post formats are to present content in a way that makes sense for a given format. For example, video posts should highlight the video. Or maybe link posts should link directly to the site you want your readers to see instead of the permalink for that article.

Here I’m going to take a look at creating different templates for specific formats.

First, I’ll say that a good way to get started with post formats is just to enable the ones you want with the appropriate call to add_theme_support() 1 in your theme’s functions.php file and customize them with CSS. If your theme takes advantage of the post_class() function to add classes to posts, each post will get a class in a style like “format-$format”, where $format is the name of the format — “format-gallery”, for example.

If you decide that you need to take it a step further and actually generate different HTML depending on the format, the following is a technique that I’ve used. Let’s start by looking at the loop in index.php of the _s theme, available on Github.

<!--?php /* Start the Loop */ ?--> <!--?php while ( have_posts() ) : the_post(); ?--> <!--?php <br ?--> /* Include the Post-Format-specific template for the content. * If you want to overload this in a child theme then include a file * called content-___.php (where ___ is the Post Format name) and that will be used instead. */ get_template_part( 'content', get_post_format() ); ?>

The most interesting part of this is get_template_part( 'content, get_post_format() ) , which will look for a file with a name like “content-$format.php”. If no such file exists, it will fall back to “content.php”, which will be our default for post types that we’re not going to specify special templates for.

If we think ahead a little bit we’ll realize that all of our posts will probably share some common structure. In my case, I want the title to be displayed if one was specified, certain meta information should be displayed, and of course I want to display the content. So I’m going to create a file called “content-base.php” with that basic structure. Then in content.php, I’ll basically just make a call to get_template_part() to include that base stucture.

In my v11 theme, “content.php” looks like:

>



Other Formats

Now we can create the templates for other formats just by adding the correct files. An example of my status format template looks like:

> echo ‘ ‘;

the_content();

echo ‘ ‘;

?>

In this case, content-meta.php just defines what meta information to display on a post.

Another example that reuses the base layout is my video format template:

> $video = get_post_meta( $post->ID, ‘_format_video_embed’, true );

if ( !empty( $video ) ) {

$url = esc_url( $video );

if ( $embed = wp_oembed_get( $url ) )

echo $embed;

elseif ( !empty( $url ) ) {

printf( ‘ ‘, $url );

} else {

echo $video;

}

} get_template_part( ‘content-base’ ); ?>

Again, there is some more complex stuff here — I use a custom field called “_format_video_embed” to define what video to use. But the general idea is that it displays a video and then just grabs the base structure.

#Tips for Links

A common way of implementing link posts is to grab the first link in the post and set that as the reference for the title link. I have a function in v11 that lets you grab the first link in a post. Then you can output that as the href in content-link.php instead of using the permalink.

if ( ! function_exists( ‘v11_url_grabber’ ) ) :

/**

* Grab the first URL in a link post so we can use it as the href

/

function v11_url_grabber( $content ) {

if ( ! preg_match( ‘/<a\s[^>]?href=\'”[\'”]/is’, $content, $matches ) )

return false;

return esc_url_raw( $matches[1] );

}

endif;

#WP Post Formats

As a final note, I like Crowd Favorite’s WP Post Formats plugin. It gives you some nice looking meta boxes for interacting with custom fields on your post formats.