Implementing Custom Post Formats in WordPress Themes

Posted on 19th June 2023

In WordPress, a post format is a piece of meta information that can be used by a theme to customize its presentation of a post. WordPress comes with a set of built-in post formats:

As of WordPress 4.7, the following post formats are available:

Standard

Aside

Image

Video

Quote

Link

Gallery

Audio

Chat

By default, a WordPress theme will use the standard post format for all post types. However, if a theme declares support for post formats, it can use a different post format for each post type. For example, a theme could use the image post format for image attachments, the video post format for video attachments, and the standard post format for all other post types.

To add post format support to a theme, the theme must first be registered for post formats using add_theme_support():

add_theme_support( ‘post-formats’, array(
‘aside’,
‘image’,
‘video’,
‘quote’,
‘link’,
) );

This code would add support for the aside, image, video, quote, and link post formats to the theme.

Once a theme has registered support for post formats, the active theme can be queried for its post format support using current_theme_supports():

if ( current_theme_supports( ‘post-formats’ ) ) {
// The theme supports post formats.
}

This code would check if the active theme supports post formats.

The next step is to actually use the post format meta information in the theme. To do this, the theme can use the get_post_format() function, which returns the post format for a given post:

$format = get_post_format( $post_id );

This code would get the post format for the post with an ID of $post_id.

Once the post format has been retrieved, the theme can use it to customize the presentation of the post. For example, a theme could use the post format to display a video attachment differently than a standard post:

if ( ‘video’ == get_post_format( $post_id ) ) {
// This is a video attachment, so display it differently.
} else {
// This is a standard post, so display it normally.
}

This code would check if the post with an ID of $post_id is a video attachment. If it is, the theme would display it differently. Otherwise, the theme would display it normally.

A theme can also use the has_post_format() function to check if a post has a given post format:

if ( has_post_format( ‘video’, $post_id ) ) {
// This post has the video post format.
}

This code would check if the post with an ID of $post_id has the video post format.

Finally, a theme can use the get_post_format_string() function to get the label for a given post format:

$format_string = get_post_format_string( $format );

This code would get the label for the post format $format.

In the previous article, we looked at how to add custom post formats to a WordPress theme. In this article, we will look at how to use custom post formats in WordPress themes.

Custom post formats were introduced in WordPress 3.1. They allow you to change the way your posts are displayed on your website. For example, you can use custom post formats to display your posts in a grid layout, or in a single column layout.

To use custom post formats in your WordPress theme, you need to add support for them in your theme. You can do this by adding the following code to your theme’s functions.php file:

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

Once you have added support for custom post formats to your theme, you can start using them in your posts. To do this, you need to add a custom field to your post. The custom field should be called “format” and the value should be the name of the custom post format you want to use.

For example, if you want to use the “aside” post format, you would add the following to your post:

format: aside

You can also use the “post-format” taxonomy to assign post formats to your posts. To do this, you need to add the following code to your theme’s functions.php file:

register_taxonomy( ‘post_format’, ‘post’, array( ‘hierarchical’ => false, ‘label’ => __( ‘Post Formats’, ‘textdomain’ ), ‘query_var’ => true, ‘rewrite’ => array( ‘slug’ => ‘post-format’ ), ‘show_ui’ => true, ‘show_admin_column’ => true ) );

Once you have registered the taxonomy, you can assign post formats to your posts using the post editor. Simply select the format you want to use from the “Post Format” dropdown menu.

Custom post formats are a great way to change the way your posts are displayed on your website. By using custom fields or the post-format taxonomy, you can easily control which format is used for each post.

The final step is to add support for your custom post formats to your theme. To do this, you need to add a few lines of code to your functions.php file.

First, you need to add support for the custom post formats you want to use. You can do this by adding the following line of code to your functions.php file:

add_theme_support( ‘post-formats’, array( ‘aside’, ‘image’, ‘video’, ‘quote’, ‘link’ ) );

Next, you need to add a callback function that will be used to display the content for each custom post format. The callback function should have the same name as the custom post format. For example, if you want to add support for the image post format, you would add a callback function named image_callback().

Your callback function should echo the HTML that you want to display for the custom post format. For example, the following callback function would display the featured image, title, and content for an image post:

function image_callback() {
the_post_thumbnail();
the_title();
the_content();
}

Finally, you need to add a line of code to your template files (e.g. single.php, page.php, archive.php, etc.) that will call the appropriate callback function based on the post format. The following line of code would call the image_callback() function for image posts:

if ( has_post_format( ‘image’ ) ) {
image_callback();
}

You can add similar lines of code for the other custom post formats you want to support.

That’s all you need to do to add support for custom post formats to your theme. With just a few lines of code, you can add support for a variety of post formats, and make it easy for your users to post content in the format that they want.