Join 9,000+ Readers

Setup your own Custom WordPress Theme Post Formats

by Jake Rocheleau

on December 27, 2012

in Wordpress

WordPress users are not often familiar with custom post formats. These are different bits of information you may use to customize the content you publish on a blog. The most notorious example of post formats is on the Tumblr dashboard where you may select a number of options including text, photo, quote, video, or audio.

featured picture - wordpress backend admin custom post formats

These same post formats are available in WordPress and may be used inside your theme files. I want to use this article as a brief introduction into using post formats inside WordPress. Web developers looking to customize formats should have at least basic knowledge of PHP. But much of the code is already out there online, so it is not difficult to study on your own.

Post Formats vs Post Types

I would like to distinguish that a format style is not the same as the post type. This could really be named a content type because “Post” is one of the available options. Post types include Posts, Pages, Revisions, Attachments, and Nav Menus.

With the release of WordPress 3.0 it is now possible for developers to create their own custom post types. So along with the defaults you may now register your own such as user links, photos, portfolio items, or anything else. This provides a powerful system which opens WordPress as a much more dynamic CMS.

The Post Formats are applied onto each post type, specifically within posts and pages. You can select a format to further customize the publication based on the available content. So if you are focusing on an image gallery it may be useful to widen the body layout. This is just one simple example, but there are hundreds of reasons to distinguish between new post formats.

Adding Theme Support

When launching right out of the box not all WordPress themes will support post formats. You must enable some of the default options from scratch inside your theme’s functions.php file. Just add this line in your file and you should notice another widget in the WP admin backend.

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

Now you will want to determine when a post format is being used inside the content area. This requires a loop object or some type of post ID value. Check out the get_post_format() method which documents exactly how to pull the value in a post template. If the current post has no formatting this function will return false.

But an easier way to determine if the current post is using formats is by checking the has_post_format() method. This will always return a boolean value of true or false, which may save you time in working through some logic on the page.

Styling the Page Body

If you have the logic to place inside your post template file then everything should work fine. You can now check what the current post format value is set to on any post or page, and apply classes onto the body element or container block. This allows you to customize elements using CSS without touching much of the HTML markup.

To avoid typing out multiple if/else clauses I found a great snippet of code on this tutorial by Dotnet Magazine. We can use the function get_template_part() and determine the current post format value. Then using this variable it is a lot easier applying classes or appending new HTML into your templates automatically. Just add the following code into your template file.

if (have_posts()) :
     while (have_posts()) : the_post();
          if(!get_post_format()) {
               get_template_part('format', 'standard');
          } else {
               get_template_part('format', get_post_format());
          }
        endwhile;
endif;

Source

Final Thoughts

I do hope this guide to WordPress post formats can be helpful for some users. This CMS is not admittedly the easiest system to sit down and learn on your own. There are a lot of moving parts, especially in the WordPress backend. However with just a bit of determined practice you can learn a lot of really handy techniques.

Spend some time publishing content using post formats and see what you can find in other freebie themes. Many developers will take these format styles into account when creating blog post template pages. There is a lot you can learn by studying other people’s code. And these methods will continue years into the future as WordPress develops even further.

About Jake Rocheleau

Jake is a freelance writer and web developer. He writes articles for numerous blogs on freelancing, Wordpress, CSS3, and jQuery. You can find him all throughout Google and follow his tweets @jakerocheleau. Connect with Jake on Google +