has_post_format()WP 3.1.0

Checks for the presence of the specified format for a post. Conditional tag.

The function takes the name of the format or several formats and checks if the post in the loop belongs to the specified format.

If a second parameter - post ID - is specified, the function can be used outside the loop - the format check will be for the specified post.

Post formats are an additional feature of WordPress that is enabled separately.

Use set_post_format() to set the format for the post.

More about post formats.

1 time — 0.001473 sec (very slow) | 50000 times — 63.94 sec (very slow)

No Hooks.

Returns

true|false.

  • true - the post belongs to the specified format;
  • false - the post does not belong to any format.

Usage

has_post_format( $format, $post_id );
$format(string/array) (required)
The name of the post format to check. You can specify multiple format names in an array. Basic WordPress formats: aside, chat, gallery, link
image, quote, status, video, audio.
$post_id(int/object)
The ID of the post (record) for which the presence of the specified format needs to be checked.
Default: null - the current post in the loop

Examples

0

#1 Demo - check format

Suppose we need to check the format of the post (inside the loop) and display an icon if it is, say, video format:

// inside the loop

if( has_post_format( 'video' ) ){ 

	echo "<img src="icon link" alt=""/>";
 }
0

#2 Example of specifying multiple formats to check

Suppose our posts are conventionally divided into news (regular posts) and media (posts with galleries and videos). When outputting in the loop, we need to mark the posts as "media". To do this, we will specify the formats as an array:

if( has_post_format( array('video', 'gallery') ) ){
	// this is a media post
}
0

#3 Checking the format of a specific post

This example shows how to check the format of a post with ID = 23:

if( has_post_format( 'status', 23 ) ){
	// it's status
}

Changelog

Since 3.1.0 Introduced.

has_post_format() code WP 6.9.1

function has_post_format( $format = array(), $post = null ) {
	$prefixed = array();

	if ( $format ) {
		foreach ( (array) $format as $single ) {
			$prefixed[] = 'post-format-' . sanitize_key( $single );
		}
	}

	return has_term( $prefixed, 'post_format', $post );
}