How to add Post Formats Support to Your Theme


Description

What are Post Formats, how can you use them, and how do you develop Post Formats? This step-by-step walkthrough will give you an understanding of what Post Formats are and how to create your own.

Prerequisite Skills

  • Basic knowledge of HTML/CSS/PHP
  • Basic knowledge of installing and activating WordPress themes
  • Understanding of how folders and files are structured
  • Ability to edit files with a text editor

Objectives

  • Students will create each post format using a default WordPress theme
  • Students will enable Post Format support on a theme that does not yet have Post Formats enabled
  • Students will style each Post Format type

Assets

  • WordPress installed locally
  • Twenty Thirteen theme installed (all post formats done and styled)
  • Twenty Twelve theme installed (to demonstrate how to enable and style Post Formats when not already supported by selected theme)
  • Child theme to enable post formats
  • Filler content from Theme Unit Test Data

It is important that you have Underscores configured as a fully functioning theme, minus the Post Formats, to demonstrate how to configure and style.

Screening Questions

  • Are you familiar with installing and activating themes via the WordPress Dashboard?
  • Do you have at least a basic knowledge of HTML/CSS/PHP?
  • Do you feel comfortable using a text editor to edit code?
  • Will you have a locally or remotely hosted sandbox WordPress site to use during class?

Teacher Notes

  • Performing a live demo while teaching the steps to make a child theme is crucial to having the material “click” for students.
  • It is easiest for students to work on a locally installed copy of WordPress. Set some time aside before class to assist students with installing WordPress locally if they need it. For more information on how to install WordPress locally, please visit Installing on a Local Server.
  • The preferred answers to the screening questions is “yes.” Participants who reply “no” to all 4 questions may not be ready for this lesson.

Hands-on Walkthrough

Introduction: What are Post Formats?

Post Formats have existed in WordPress since 3.1. However, few themes natively support it, and fewer users implement it. A Post Format is a piece of meta information that can be used by a theme to customize its presentation of a post. The Post Formats feature provides a standardized list of formats that are available to all themes that support the feature. Themes are not required to support every format on the list. New formats cannot be introduced by themes or even plugins. The standardization of this list provides both compatibilities between numerous themes and an avenue for external blogging tools to access this feature in a consistent fashion. In short, with a theme that supports Post Formats, a blogger can change how each post looks by choosing a Post Format from a radio-button list. Types of Post Formats include:

Aside – Typically styled without a title. Similar to a Facebook note update.

Aside post screenshot

Gallery – A gallery of images. Post will likely contain a gallery shortcode and will have image attachments.

Gallery post screenshot

Link – A link to another site. Themes may wish to use the first 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.

Link post type screenshot

Image – A single image, with or without a caption. The first 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.

Image post type screenshot

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.

Quote. post type scree
Quote post type
Status post type.

Video – A single video or video playlist.

Why Use Post Formats?

Using Post Formats, your blog visitors will know exactly what type of content they are viewing.  Different styles for each Post Format will make your blog more visually interesting. Previously, developers tried to accomplish these unique styles for content by modifying categories or by using custom post types.  These custom types were intended to be something other than blog content. Using Post Formats will keep your content all within the blog portion of your site, and once configured, will require only that you check which format you are using. Post Formats will show up in your RSS feed, and will be editable within the Posts part of your site.

How Post Formats Work

When you edit an individual post, you’ll see a Format module:

Post formats in the editor.

If you do not see the Format module, check that you have it enabled in your Screen Options. If you don’t see the Format option there, then you are likely using a theme that does not use post formats

Once you’ve assigned posts to a particular Post Format, you will see icons in the dashboard indicating which format each post is.  Go to Posts > All Posts:

Post formats in dashboard
Post formats in dashboard

When viewing your blog, the content will have a unique look for each format.

Post formats in site front-end.

Adding Theme Support

Themes need to use add_theme_support() in the functions.php file to tell WordPress which post formats to support by passing an array of formats like so:

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

Please note that you must call this before the init hook gets called! A good hook to use is the after_setup_theme hook.

Adding Post Type Support

If you would like to use Post Formats styling on Pages or Custom Post Types, it is possible. Post Types need to use add_post_type_support() in the functions.php file to tell WordPress which post formats to support:

// add post-formats to post_type 'page'
add_post_type_support( 'page', 'post-formats' );

// add post-formats to post_type 'my_custom_post_type'
add_post_type_support( 'my_custom_post_type', 'post-formats' );

Using Post Formats

In the theme, make use of get_post_format() to check the format for a post, and change its presentation accordingly. Note that posts with the default format will return a value of FALSE. Or make use of the has_post_format() conditional tag:

if ( has_post_format( 'video' )) {
  echo 'this is the video format';
}

An alternate way to use formats is through styling rules. Themes should use the post_class() function in the wrapper code that surrounds the post to add dynamic styling classes. Post formats will cause extra classes to be added in this manner, using the “format-foo” name. For example, one could hide post titles from status format posts by putting this in your theme’s stylesheet:

.format-status .post-title {
display:none;
}

Styling Post Formats

Although you can style and design your formats to be displayed any way you see fit, each of the formats lends itself to a certain type of “style”, as dictated by modern usage. It is well to keep in mind the intended usage for each format, as this will lend them towards being easily recognized as a specific type of thing visually by readers. For example, the aside, link, and status formats will typically be displayed without title or author information. They are simple, short, and minor. The aside could contain perhaps a paragraph or two, while the link would probably be only a sentence with a link to some URL in it. Both the link and aside might have a link to the single post page (using the_permalink()) and would thus allow comments, but the status format very likely would not have such a link. An image post, on the other hand, would typically just contain a single image, with or without a caption/text to go along with it. An audio/video post would be the same but with audio/video added in. Any of these three could use either plugins or standard Embeds to display their content. Titles and authorship might not be displayed for them either, as the content could be self-explanatory. The quote format is especially well suited to posting a simple quote from a person with no extra information. If you were to put the quote into the post content alone, and put the quoted person’s name into the title of the post, then you could style the post so as to display the_content() by itself but restyled into a blockquote format, and use the_title() to display the quoted person’s name as the byline. A chat in particular will probably tend towards a monospaced type display, in many cases. With some styling on the .format-chat, you can make it display the content of the post using a monospaced font, perhaps inside a gray background div or similar, thus distinguishing it visually as a chat session. See Post Formats: Chat for further styling.

Formats in a Child Theme

Child Themes inherit the post formats defined by the parent theme. Calling add_theme_support() for post formats in a child theme must be done at a later priority than that of the parent theme and will override the existing list, not add to it.

add_action( 'after_setup_theme', 'childtheme_formats', 11 );
function childtheme_formats(){
     add_theme_support( 'post-formats', array('aside', 'quote', 'status', 'image', 'gallery', 'chat', 'link', 'audio', 'video') ); }

Calling remove_theme_support('post-formats') will remove it all together.

Group Exercises

  1. Using Twenty Thirteen theme, create a post using each post format option.
  2. Using Underscores:
    1. Create each Post Format

Model after Twenty Thirteen theme files but remember to change calls to the theme naming convention to your own theme

  1. content-aside.php
  2. content-audio.php
  3. content-chat.php
  4. content-gallery.php
  5. content-image.php
  6. content-link.php
  7. content-quote.php
  8. content-status.php
  9. content-video.php
  10. content-none.php

Style Post Formats (model after Twenty Thirteen Style.css line 1380)

Quiz

  1. What are Post Formats? Post Formats are used by a theme to customize its presentation of a post. The Post Formats feature provides a standardized list of formats that are available to all themes that support the feature.
  2. What are several Post Format options? Aside, audio, chat, gallery, image, link, quote, status, video
  3. Are Post Formats the same as Custom Post Types? When would you pick a Custom Post Type instead of a Post Format? No, custom post types appear as another option on the admin dashboard near posts and pages.  Custom Post Types are useful when you don’t want the content to appear on /blog, such as an events calendar or real estate listings.
  4. Where do you enable Post Formats for your theme? Functions.php, specifically in a child theme functions.php
  5. What must you do when writing a post that should use a Post Format? Select which post format to use
  6. Where do you customize the look for Post Formats? Style.css, or specifically the child theme style.css
  7. Do all themes support Post Formats? No
  8. What default WordPress themes support Post Formats? Twenty Thirteen and Twenty Fifteen
  9. Can you add Post Format support to your theme or child theme? Yes