What are WordPress Post Formats, why & how to use them?

Post Formats is a theme feature introduced in WordPress with version 3.1, means it will only be available if your theme supports it. A post format is a meta information that can be used by themes to categorize the posts to present them differently.

Why use Post Formats?

Suppose you post an image in a blog post and an article in another post and you want to present the image post in a different way than a normal article. Without “Post Formats” you will think of creating categories like article,video and assigning posts to them, then while displaying the post use conditional checks like in_category(‘video’) to format the display or you will think of styling based on “post_class()”.

With “Post Formats” you get a meta box on the add/edit post screen where you can select the Post Format for a blog post while creating or editing it; your theme files can then use this post meta data to style your posts differently.

How to enable Post Formats in your WordPress theme?

To enable support for Post Format in your theme use the following code in your theme functions.php file


add_theme_support( 'post-formats', array( 'gallery', 'link', 'image' ) );

Alternatively you can use the Plugin hook “after_setup_theme” as below:


add_action( 'after_setup_theme', 'mytheme_formats' );

function mytheme_formats(){

add_theme_support( 'post-formats', array( 'aside', 'gallery', 'link' ) );

}

This code adds support for the following post formats – ‘gallery’, ‘link’, ‘image’.

What are the Post Formats I can use?

The post formats available for use are pre-defined by WordPress and is available at CODEX [https://codex.wordpress.org/Post_Formats].

Here is the complete list:

  • aside – Typically styled without a title. Similar to a Facebook note update.
  • gallery – A gallery of images. Post will likely contain a gallery shortcode and will have image attachments.
  • link – A link to another site. Themes may wish to use the first <a href=””> tag in the post content as the external link for that post. An alternative approach could be if the post consists only of a URL, then that will be the URL and the title (post_title) will be the name attached to the anchor for it.
  • image – A single image. The first <img /> tag in the post could be considered the image. Alternatively, if the post consists only of a URL, that will be the image URL and the title of the post (post_title) will be the title attribute for the image.
  • quote – A quotation. Probably will contain a blockquote holding the quote content. Alternatively, the quote may be just the content, with the source/author being the title.
  • status – A short status update, similar to a Twitter status update.
  • video – A single video. The first <video /> tag or object/embed in the post content could be considered the video. Alternatively, if the post consists only of a URL, that will be the video URL. May also contain the video as an attachment to the post, if video support is enabled on the blog (like via a plugin).
  • audio – An audio file. Could be used for Podcasting.
  • chat – A chat transcript, like so:

Can I use a Custom Post Format?

No, custom post formats are not supported by wordpress.

How to make use of Post Formats in theme?

1. Using the conditional tag: has_post_format()

In the posts loop we can use has_post_format() to check if a post format has been assigned to post or not and then display/format the post accordingly.


if ( has_post_format( 'gallery' ) {

// code to display the gallery format post here

} else if (has_post_format('link')) {

// stuff to display the link format post here

} else if (has_post_format('image')) {

// stuff to display the image format post here

}else {

// code to display the normal/standard format post here

}

2. Using get_post_format()

get_post_format() returns the format of the post, or false if no format is set.

WordPress ‘twentytwelve’ and ‘twentythirteen’ support post formats. These themes make use of get_post_format(), check posts loop in the index.php file.

Here is the loop part from twentythirteen theme:


<?php if ( have_posts() ) : ?>

 

<?php /* The loop */ ?>

<?php while ( have_posts() ) : the_post(); ?>

<?php get_template_part( 'content', get_post_format() ); ?>

<?php endwhile; ?>

 

<?php twentythirteen_paging_nav(); ?>

 

<?php else : ?>

<?php get_template_part( 'content', 'none' ); ?>

<?php endif; ?>

This code diplays the code in files like content-video.php, content-image.php, content-gallery.php etc according to the post format.

Lets open the file content-image.php, it has the following post wrapper code


<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

Since this file for post format image, post_class() function will add a class “format-image” to this post wrapper. If the post format video is video, it will add class format-video, if post format is quote, it will add class format-quote and so on. We can use these classes to add css based on post formats.

Therefore we should use always use post_class() function in the wrapper code that surrounds the post to add dynamic styling classes.

NOTES:

1. When writing or editing a Post, Standard is used to designate that no Post Format is specified. Also if a format is specified that is invalid then standard (no format) will be used.

2. Selecting a Post Format while creating a post does not mean that it gets styled differently, you need to use this information in you theme to style your posts.

3. Post Format and Post Type are not the same thing.

4. Post Format support can be added for Custom post types also.

Leave a Comment

Back to top