Make WordPress Custom Post Type Support Post Formats

business coffee composition computer

To allow a custom post type to support post formats, the ‘supports’ parameter needs to include ‘post-formats’ when registering this post type.

If the post type already exists, you may need to use the add_post_type_support() function to add support. This function takes two parameters, the first is the name of the post type, and the second is the feature to be added.

Here is an example, this code adds the ‘post-formats’ feature to ‘my_post_type’:

function add_post_formats_to_my_post_type() {
    add_post_type_support( 'my_post_type', 'post-formats' );
add_action( 'init', 'add_post_formats_to_my_post_type' );

This code should be added to the theme’s functions.php file (or in a plugin for deploying code like Snippets). This code has the effect of adding post type support when WordPress initializes (‘init’ hook).

Also, you need to make sure that the theme being used supports post formats. You can add the following code to the theme’s functions.php file to enable support for post formats:

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

The above example code enables the ‘aside’ and ‘gallery’ post formats, you can add other post formats as needed.

For example, if you want to enable all available post formats, it would be:

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

In summary, three things need to be ensured:

  1. The theme supports post formats,
  2. The post type supports post formats,
  3. And the theme has appropriate templates for specific post formats.

Discover more from Sang-nguok

Subscribe now to keep reading and get access to the full archive.

Continue reading