Post Type Support in wordpress

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', 'gallery' ) );
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

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_action('init', 'my_theme_slug_add_post_formats_to_page', 11);

function my_theme_slug_add_post_formats_to_page(){
add_post_type_support( 'page', 'post-formats' );
register_taxonomy_for_object_type( 'post_format', 'page' );
}
Next example registers custom post type 'my_custom_post_type', and add Post Formats.
// register custom post type 'my_custom_post_type'
add_action( 'init', 'create_my_post_type' );
function create_my_post_type() {
register_post_type( 'my_custom_post_type',
array(
'labels' => array( 'name' => __( 'Products' ) ),
'public' => true
)
);
}

//add post-formats to post_type 'my_custom_post_type'
add_post_type_support( 'my_custom_post_type', 'post-formats' );
Or in the function register_post_type(), add 'post-formats', in 'supports' parameter array. Next example is equivalent to above one.
// register custom post type 'my_custom_post_type' with 'supports' parameter
add_action( 'init', 'create_my_post_type' );
function create_my_post_type() {
register_post_type( 'my_custom_post_type',
array(
'labels' => array( 'name' => __( 'Products' ) ),
'public' => true,
'supports' => array('title', 'editor', 'post-formats')
)
);
}

Using 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;
}

Suggested Styling

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.

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', 'gallery', 'link' ) );
}
Calling remove_theme_support('post-formats') will remove it all together.

Backwards Compatibility

If your plugin or theme needs to be compatible with earlier versions of WordPress, you need to add terms named post-format-$format to the "post_format" taxonomy. For example,
wp_insert_term( 'post-format-aside', 'post_format' );
You must also register the post_format taxonomy with register_taxonomy().

Related Post


Latest Post


Recent Posts Widget

Make sure to never miss a thing...

Get the latest news from the creative industry along with other creative goodies, conveniently delivered to social media.