Getting The Most of Post Formats: Using Post Formats In The Loop

In the first part of this series I discussed what post formats are and why you should use them. Now it is time to learn how to use post formats to control the main loop in your theme in order to display each type of post properly.


Enabling Post Formats

Before you can use post formats in a theme you must register support for them. Each post type, built-in (that is, posts and pages) or custom post types need to have support for post types registered separately. 

For posts you use the function add_theme_support() as part of your theme setup in functions.php, like this:

For other post types, including pages and custom post types, you would use add post_type_support(). If you want pages to support the same post formats that posts do you would simply tell WordPress that you want it to like this:

You can use similar code to add support for custom post types, or you add 'post-formats' to the 'supports' argument in register_post_type() when you create the post type.

Different Post Formats For Different Post Types

In the previous section the code I showed you added support to other post types the same post formats as were enabled for posts. If you wanted your custom post type to support different post formats, that is possible as well using add_post_type_support() by simply passing an array of post formats to it.

For example, to add support for galleries and images to pages, while adding video to a custom post type called 'products' and all post formats to posts, you would do this:


Using Post Formats In The Loop

Now that your theme supports post formats, it's time to learn how to use them.

Introducing get_template_part()

What makes post formats cool is that we can have one main, loop, with our different markup for each post format in separate files. This functionality is thanks to two functions, the first of which you should familiarize yourself with is get_template_part().

While it does use require(), if you trace back far enough in the source code, get_template_part() gives us a more flexible, and more forgiving way to include template files in other templates that we do not get with require(). I say forgiving because, as opposed to require(), get_template_part() can work with file paths that don't exist.

For example, get_template_part( 'content', 'foo' ); will attempt to load the file content-foo.php, but if it doesn't exist will then attempt to load content.php instead. In contrast, require( 'content-foo.php' ); will return an error if content-foo.php does not exist, whether content.php exists or not.

Keep in mind that get_template_part() is tailored for this specific use as it loads files relative to the current theme's root. As a result you don't need to use get_template_directory_uri() or any similar functions to specify the path. You can still use it to load files in subdirectories of the theme's main directory, by specifying the path, relative to the theme's root, in the first argument. For example, to load content-main.php from a subdirectory called 'parts' you would use get_template_part( 'parts/content', 'main' );.

Introducing get_post_format()

The second function that makes this all possible is get_post_format(), which returns the post format for the current post in the loop. While it has many uses, this functions main use is as an argument for get_template_part(). It is what allows us to include content parts specific to a post format as we will see in the next section.

Because of the flexibility of get_post_format() discussed in the last section, we are able to use get_post_format() as one of its arguments, even if there isn't a content part in the theme for all of the post formats in use.

Including Template Parts In The Loop

Now that you understand the two functions that make post format specific content parts possible, let's put it all together. 

Take a look at the main loop from Twenty Fourteen:

As you can see this is a very simple loop. The actual markup for the posts are contained in the individual content parts. Because of the forgiving nature of get_template_part() if a content part for a specific post format doesn't exist, WordPress will fall back to content.php. That means that if the current post has the format 'video' WordPress will first attempt to load content-video.php, and if that is not found, then it will fallback to content.php.

Child Themes and Content Parts

As I stated earlier, get_template_part() is child theme aware. That means that WordPress will first look for a file in the child theme's folder before the parent theme, if a child theme is in use.

As a result, it is a good practice to use get_template_part( 'content', get_post_format() ); to include your main loop in your page, even if your theme doesn't support post formats. That way a child theme can declare support for a post format, and add its own content parts for the post formats it supports.


Go Forth And Format Posts

Now that you know how to add support to your theme for post formats and to set different template parts based on the post format its time to start thinking of how to put them to use in your theme. I will be covering how to do so in the remainder of this series. Be sure to also check out the code of themes, like Twenty Fourteen, that implement post formats well to get inspiration.

Tags:

Comments

Related Articles