has_post_format() WordPress Function

The has_post_format() function is used to check if a post has a specific post format. It can be used within the WordPress loop to check if the current post has the post format you specify.

has_post_format( string|string[] $format = array(), WP_Post|int|null $post = null ) #

Check if a post has any of the given formats, or any format.


Parameters

$format

(string|string[])(Optional) The format or formats to check.

Default value: array()

$post

(WP_Post|int|null)(Optional) The post to check. Defaults to the current post in the loop.

Default value: null


Top ↑

Return

(bool) True if the post has any of the given formats (or any format, if no format specified), false otherwise.


Top ↑

More Information

Usage:
$format = has_post_format($format, $post_id);

Top ↑

Source

File: wp-includes/post-formats.php

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


Top ↑

Changelog

Changelog
VersionDescription
3.1.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by the Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.