get_post_format()
Gets the format of the current or specified post: quote, video, audio.
The function determines which of the predefined formats (aside, gallery, link, image, quote, status, video, audio) the post belongs to. If the format is not specified (i.e., the "standard" is used), the function will return false.
Used within the WordPress Loop.
In some cases, you may find it useful to set the format of a regular post to standard, you can do this like so:
$format = get_post_format() ?: 'standard';
The post format is stored in the post_format taxonomy. The function returns the term slug without the prefix post-format-.
Post formats are an additional functionality of WordPress that is enabled separately via add_theme_support('post-formats').
More about post formats.
No Hooks.
Returns
String|false.
String— format: 'aside', 'gallery', etc.false— no format is specified for the post.
Usage
$format = get_post_format( $post_id );
- $post_id(integer)
- ID of the post whose format you want to get.
Default: current post in the loop
Examples
#1 Output different content, depending on the Post Format.
Make sure there is a file for the default format format.php and files for other formats respectively: format-link.php, format-aside.php, etc.
This code should be used inside a WordPress loop:
$format = get_post_format(); get_template_part( 'format', $format );
Changelog
| Since 3.1.0 | Introduced. |
get_post_format() get post format code WP 6.8.3
function get_post_format( $post = null ) {
$post = get_post( $post );
if ( ! $post ) {
return false;
}
if ( ! post_type_supports( $post->post_type, 'post-formats' ) ) {
return false;
}
$_format = get_the_terms( $post->ID, 'post_format' );
if ( empty( $_format ) ) {
return false;
}
$format = reset( $_format );
return str_replace( 'post-format-', '', $format->slug );
}