get_post_format( int|object|null $post = null )

Retrieve the format slug for a post


Description Description


Parameters Parameters

$post

(int|object|null) (Optional) Post ID or post object. Optional, default is the current post from the loop.

Default value: null


Top ↑

Return Return

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


Top ↑

Source 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

Changelog
Version Description
3.1.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Codex

    Usage in Templates

    /*
     * Pull in a different sub-template, depending on the Post Format.
     * 
     * Make sure that there is a default format.php file to fall back to as a default.
     * Name templates format-link.php, format-aside.php, etc.
     *
     * You should use this in the loop.
     */
    get_template_part( 'format', get_post_format() );
    
  2. Skip to note 2 content
    Contributed by Codex

    Example

    /*
     * Pull in a different sub-template, depending on the Post Format.
     * 
     * Make sure that there is a default 'format.php' file to fall back to
     * as a default. Name templates 'format-link.php', 'format-aside.php', etc.
     *
     * You should use this in the loop.
     */
    get_template_part( 'format', get_post_format() );
    

You must log in before being able to contribute a note or feedback.