get_post_format() WordPress Function

The get_post_format() function allows you to get the post format for a given post.

get_post_format( int|WP_Post|null $post = null ) #

Retrieve the format slug for a post


Parameters

$post

(int|WP_Post|null)(Optional) Post ID or post object. Defaults to the current post in the loop.

Default value: null


Top ↑

Return

(string|false) The format if successful. False otherwise.


Top ↑

More Information

This will usually be called in the the loop, but can be used anywhere if a post ID is provided.

The set of currently defined formats are:

  • aside
  • chat
  • gallery
  • link
  • image
  • quote
  • status
  • video
  • audio

Note also that the default format (i.e., a normal post) returns false, but this is also referred in some places as the ‘standard’ format. In some cases, developers may wish to do something like the following to maintain consistency:

$format = get_post_format() ? : 'standard';

 

 


Top ↑

Source

File: wp-includes/post-formats.php

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


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.