Using WordPress Post Formats

written by William Patton on July 10, 2019 in WordPress Custom with 3 comments

Theme Developers can use WordPress Post Formats to provide special handling for certain types of post content. It makes it easy for developers to handle the different types and provides an easy menu for users to select which type they want to apply when they write a post.

Post Formats an are optional feature which themes can support. It is the theme authors decision what formats they want to enable for their theme.

Uses For WordPress Post Formats

As a theme developer there are a number of different ways you can use the Post Formats feature. Creativity can be used here to provide all kinds of custom layouts or display types. The post author just has to select which format they want at the time of writing the post and the theme can do it’s magic.

A few ways I’ve seen post formats used:

  • To force only featured image to show for galleries when they are inside larger post lists/archive pages.
  • To allow Social Network style status updates on private pages for collaboration on membership sites.
  • As an easy filtering mechanism in addition to categories and tags so you can have specific pages dedicated to Videos or Links or any of the other formats.
  • Having special custom post format single pages – for instance you could have an image format post use 100% of the available width on the page, removing the sidebar completely.

There are near limitless ways to utilise the post formats if you approach it with a little creativity.

List of all WordPress Post Formats

WordPress has a list of built-in standardised Post Formats. You can not add to this list through a theme, or even a plugin. This is to ensure they are kept standardised for end users and makes it a somewhat easier transition between themes. It also allows alliterative software the ability to utilise post formats when posting to WordPress from external editors because they can know the expected values. Even when the them

  • aside – Typically styled without a title. Can be used somewhat like a note or a mini-post.
  • gallery – A gallery of images. Post will likely contain a gallery of some kind, possibly via a shortcode, and almost certainly will have image attachments.
  • link – A link to another place or 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 simplest way to handle this is with the featured image set for the post. Alternitivaely the first <img /> tag in the post could be considered the image or 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:

Enabling Post Formats on your WordPress Site.

Post Formats are enabled inside of a theme by using an add_theme_support() call and passing it the post-formats string and an array of formats you want enabled. You do not have to enable all of the formats.

Once post formats are enabled the list of supported formats appears inside the editor as a set of radio buttons to select from. You can change the post format of any post to any other supported format whenever you want.

Post Format support can be added to your theme in your themes functions.php file with one line of code.

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

That code adds an aside, gallery and link Post Formats to your theme – but you can add any combination of the formats listed above. Once enabled these are available to choose via a radio button when in the post editor.

Ideally you want to wrap that call inside an action appropriate for themes to load things in. Most of the time the best action for that is the after_setup_theme action.

Child Theme’s functions.php

You can add Post Formats via child themes too (and I highly recommend you use a child theme for edits) but the process is slightly different due to the order that the functions.php file is loaded.

The first thing to know is that any time the add_theme_support( 'post-formats' array() ) OVERWRITES any previous call to it. Since the functions.php file is special – in that it loads from both the parent and the child – you need to make sure that using a child theme you call it later than the parent. Since the child theme functions.php file is loaded before the parents that becomes a bit trickier.

The simplest way around that is to run your call inside of a hook that has a priority later than the default.

Code like this will work where we set the priority to 11 – any number higher than 10 should work.

function childtheme_add_post_formats(){
add_theme_support( 'post-formats', array( 'aside', 'gallery', 'link' ) );
}

add_action( 'after_setup_theme', 'childtheme_add_post_formats', 11 );

Using Post Formats in your Theme

<?php
/* Post Type Selector */
if ( ( has_post_format( 'link' ) ) {
/* Custom code for 'link' post format. */
} elseif ( ( has_post_format( 'aside' ) ) {
/* Custom code for the 'aside' post format. */
} else {
/* Default code goes here. */
}?>

As you can see it’s as easy as using if else statement in your theme and telling it what code to execute upon that condition. My suggestion would be to use this selector on your front page and upon the post hitting the condition (be it image or video or whatever) calling a corresponding template file containing the loop for just that specific post format.

Have you used Post Formats before? Managed to create anything unique with them? Do you have an idea you want to talk through with someone? Then leave a comment below and I’ll get back to you ASAP.


Notice: WP_Query was called with an argument that is deprecated since version 3.1.0! caller_get_posts is deprecated. Use ignore_sticky_posts instead. in /var/www/html/wp-includes/functions.php on line 4080