Use Post Formats to Show/Hide Content

An example of post formats being used

In WordPress 3.1, we saw the introduction of Post Formats, which allow you to style posts differently depending on their content.

For example, you can use the image Post Format to make the image much larger and move the post content below it, so that the emphasis is on the image. Or you could use the audio Post Format for podcasting and displaying the audio file in a player.

How Post Formats work

Post Formats don’t require any extra theme files. Instead, they rely on modifications to the single.php file, to dictate what should or shouldn’t be done when a particular Post Format is used.

For example, you might code your single.php to work such that, if your Post Format is ‘image’, a large image will be display at the beginning of the post: in any other case, it will be ignored.

If you’re skittish about editing your single.php file, you should take a backup first, or you could use a child theme, so that you don’t even have to touch your original theme files – you can just copy them and edit the copy, without fear of losing your changes.

Change display for one content type

Before you can implement any of these features, you need to ‘activate’ the post formats you want to use by editing your functions.php file. Add the following code to your functions.php file to enable the listed post formats:

The full list of supported post formats is shown below, so you can add as many as you need.

  • aside
  • audio
  • chat
  • gallery
  • image
  • link
  • quote
  • status
  • video

Let’s start off simple and change the display for just one content type. Here’s a snippet of a single.php file before it has been modified, which shows the date that the post was published:

Now I’m going to show you how to include the author name before the publish date when using the ‘aside’ post format:

When you set it up this way, the code says that if the post format is aside, WordPress should publish it with the author name as well as the publish date. For any other case, it will ignore the author link and just show the publish date.

Removing content based on post format

In this following example, I’ve modified the code slightly so that for all post formats, both the publish date and the author link shall be shown. However, when the aside post format is used, the author link will be ignored. All it takes is a simple exclamation mark to change it from the example above.

Several different conditions for different post formats

What about if you have a situation where the display will vary based on numerous different post formats. Take the example below. I’ve written a little snippet which will display the date differently for three post formats and will use yet another date format if none of those post formats are selected for the post.

Where have you implemented post formats? Have you come up with some creative use for them? As always, please comment with your thoughts, opinions and questions!

2 thoughts on “Use Post Formats to Show/Hide Content”

  1. Paul says:

    I wasn’t sure if post-formats could be used in single.php but with this you just confirm that it is possible. Thanks!

    1. Absolutely. You need to activate the post formats and then tell WordPress how to display then in single.php

Leave a Reply