get_post_format()WP 3.1.0

Retrieve the format slug for a post

1 time — 0. sec (speed of light) | 50000 times — 2.73 sec (fast)

No Hooks.

Return

String|false. The format if successful. False otherwise.

Usage

get_post_format( $post );
$post(int|WP_Post|null)
Post ID or post object.
Default: current post in the loop

Examples

0

#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() code WP 6.4.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 );
}